.net Model First with DbContext,无法初始化新的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5940616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:33:36  来源:igfitidea点击:

Model First with DbContext, Fails to initialize new DataBase

.netentity-frameworkentity-framework-4.1ef-code-firstef-model-first

提问by ?ukasz Baran

I give up. I found this: http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspxAnd thought, that's cool. So I quickly redesigned my model to take advantage of best of two worlds.

我放弃。我发现了这个:http: //blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx并且认为,这很酷。所以我很快重新设计了我的模型,以充分利用两个世界的优点。

But now my model fails at creating new database (or adding tabls to existing one). I get this error:

但是现在我的模型无法创建新数据库(或向现有数据库添加表)。我收到此错误:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

如果在代码优先模式下使用,使用 T4 模板生成的用于数据库优先和模型优先开发的代码可能无法正常工作。要继续使用数据库优先或模型优先,请确保在执行应用程序的配置文件中指定了实体框架连接字符串。要使用这些从 Database First 或 Model First 生成的类,使用 Code First 添加使用属性或 DbModelBuilder API 的任何其他配置,然后删除引发此异常的代码。

At:

在:

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

And that's my connection string:

这是我的连接字符串:

    <add name="ForumContextContainer"
 providerName="System.Data.SqlClient"
 connectionString="Data Source=.\SQLExpress; Initial Catalog=iForum; Integrated Security=True"/>

Note. I added Initial Catalog later, to try if it going to work, but It was exactly the same.

笔记。我后来添加了初始目录,以尝试它是否可以工作,但它完全相同。

回答by Ladislav Mrnka

This is wrong connection string. Once you are using model-first / database-first (EDMX) you must use Entity connection stringwith 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.

这是错误的连接字符串。使用模型优先/数据库优先 (EDMX) 后,您必须使用实体连接字符串来引用 .ssdl、.msl 和 .csdl 元数据文件。另请注意,从 EDMX 创建模型时,您必须在设计时创建数据库 = 您必须生成 SQL 脚本并执行它以创建数据库。

回答by Gabriel P.

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).

我猜当有人首先将 EDMX/db 添加到解决方案中的类库时,通常会出现此错误。如果这样做,请确保在类库项目的 App.config 文件中添加的连接字符串在 web.config 或 exe 项目配置文件中可用(因此只需将其复制/粘贴到那里)。

回答by Saineshwar

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=Pass3;
MultipleActiveResultSets=True;
App=EntityFramework&quot;" 
providerName="System.Data.EntityClient" />

EFmodel is my .edmx file name.

OrderDB is database name.

回答by Bashar Abu Shamaa

Delete or comment this :

删除或评论这个:

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

And change your connection string to valid one.

并将您的连接字符串更改为有效的字符串。