How to work with LocalDB and EF, without using migrations

后端 未结 2 1911
醉话见心
醉话见心 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:20

    The SQL Server Object Explorer window can hold a connection to the database. Closing the window and all windows opened from it releases the connection.

    0 讨论(0)
  • 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<SomeContext>());
    

    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<SomeContext>());
    
    0 讨论(0)
提交回复
热议问题