How to disable automatic table creation in EF 5.0?

别等时光非礼了梦想. 提交于 2019-12-12 07:44:21

问题


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.


回答1:


Use this on your application startup to turn off database initialization and migrations:

Database.SetInitializer<YourContextType>(null);



回答2:


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<YourContext >(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.



来源:https://stackoverflow.com/questions/10725712/how-to-disable-automatic-table-creation-in-ef-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!