使用 DbContext 进行模型优先,无法初始化新数据库

发布于 2024-11-06 04:19:15 字数 1022 浏览 0 评论 0原文

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

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

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

在:

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

这是我的连接字符串:

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

注意。我后来添加了初始目录,以尝试它是否有效,但它是完全相同的。

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尸血腥色 2024-11-13 04:19:15

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

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.

南城追梦 2024-11-13 04:19:15

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

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

花桑 2024-11-13 04:19:15
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="
data source=SAI-PC;
initial catalog=OrderDB;
user id=sa;
password=Pass$123;
MultipleActiveResultSets=True;
App=EntityFramework"" 
providerName="System.Data.EntityClient" />

EFmodel is my .edmx file name.

OrderDB is database name.
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="
data source=SAI-PC;
initial catalog=OrderDB;
user id=sa;
password=Pass$123;
MultipleActiveResultSets=True;
App=EntityFramework"" 
providerName="System.Data.EntityClient" />

EFmodel is my .edmx file name.

OrderDB is database name.
半城柳色半声笛 2024-11-13 04:19:15

删除或评论这一点:

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

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

Delete or comment this :

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

And change your connection string to valid one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文