How to work with LocalDB and EF, without using migrations

牧云@^-^@ 提交于 2019-12-03 07:29:57

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.

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