How to disable automatic table creation in EF 5.0?

后端 未结 2 1937
囚心锁ツ
囚心锁ツ 2021-02-13 04:07

I installed Entity Framework 5.0 RC for Framework 4.0 in my project. But when I try to get data from Views I get error. EF tries creating table for this entity.

2条回答
  •  礼貌的吻别
    2021-02-13 04:27

    If you want to turn off database initialization/migration completely regardless of in which project you're using your Context you can add a static constructor to your context to call the initializer. This ensures that the SetInitializer will be called once prior to the first construction/use of your context.

    public class YourContext : DbContext
    {
        static YourContext()
        {
            // don't let EF modify the database schema...
            Database.SetInitializer(null);
        }
    
        public YourContext() : base("name=YourContext")
        {}
        ...
    }
    

    However, if you only want to do this in a select few projects, you're better off doing it explicitly via application startup - e.g. during your normal IoC setup, like suggested by Ladislav.

提交回复
热议问题