How to disable code-first feature in EF (MVC4 Visual Studio 2012)

后端 未结 1 986
有刺的猬
有刺的猬 2020-12-16 13:59

How to disable code-first feature in EF (Visual Studio 2012)

I am using Visual Studio 2012, MVC4 (Internet application template).

I want to use EF, but not w

相关标签:
1条回答
  • 2020-12-16 15:00

    You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:

    Database.SetInitializer<MyDbContext>(null);
    

    It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.

    You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620

    Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878

    An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

    0 讨论(0)
提交回复
热议问题