Model First with DbContext, Fails to initialize new DataBase

一曲冷凌霜 提交于 2019-11-27 13:57:18

This is wrong connection string. Once you are using model-first / database-first (EDMX) you must use Entity connection string with referencing .ssdl, .msl and .csdl metadata files. Also be aware that you must create your database in design time when creating model from EDMX = you must generate SQL script and execute it to create the database.

I guess this error usually appears when someone adds the EDMX/db first to a class library in the solution. If you do so make sure that the connection string added in App.config file in the class library project is available in the web.config or the exe project config file (so just copy/paste it there).

Add this connection string to web config and make changes:

<add name="Entities" 
connectionString="
metadata=res://*/EFmodel.csdl|res://*/EFmodel.ssdl|res://*/EFmodel.msl;
provider=System.Data.SqlClient;provider 
connection string=&quot;
data source=SAI-PC;
initial catalog=OrderDB;
user id=sa;
password=Pass$123;
MultipleActiveResultSets=True;
App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

EFmodel is my .edmx file name.

OrderDB is database name.

Delete or comment this :

//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
//    throw new UnintentionalCodeFirstException();
//}

And change your connection string to valid one.

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