How to work with LocalDB and EF, without using migrations

后端 未结 2 1910
醉话见心
醉话见心 2021-02-09 06:09

I am on VS 2012 RTM, with EF 5. I am doing Code First, but trying to ignore Code Migrations since I am just in development. To avoid them, I have this set

Databa         


        
2条回答
  •  一向
    一向 (楼主)
    2021-02-09 06:35

    I had the same problem and managed to fix it. The main problem is that EF won't automatically create the database. The solution is not very hard.

    First, go to the Solution Explorer and manually create the database. To do this, go to the folder where your database is located (usually App_Data). Right-click on this folder and click on Add -> New Item. Select the Data tab and select SQL Server Database. Make sure you enter the name Entity Framework expects (which is specified in the Cannot attach the file '{0}' as database '{1}' error message).

    Then most likely you will get an error message that says that the database does not contain any model metadata. To circumvent this, change your database initialisation to:

    Database.SetInitializer(new DropCreateDatabaseAlways());
    

    Now run the application and the database should be there with tables correctly created. Once this step has been successfully executed, you change change your database initializer back to:

    Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
    

提交回复
热议问题