该上下文在Code First模式下与从EDMX文件生成的代码一起用于Database First或Model First开发。 [英] The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development

查看:164
本文介绍了该上下文在Code First模式下与从EDMX文件生成的代码一起用于Database First或Model First开发。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将最初使用EF4开发的项目迁移到EF6,以利用EF6事务管理。



我面临的问题是该项目具有是使用数据库优先方法创建的,因此当我使用 context.Database.UseTransaction 这样的代码时,遇到以下错误:

 在代码优先模式下使用上下文,该上下文使用从EDMX文件生成的代码,用于数据库优先或模型优先开发。 

此异常在 OnModelCreating 方法的内部触发我的 DbContext 类。



任何想法吗?



谢谢



编辑:



问题是使用EDMX和数据库优先方法。我必须在这个项目中实现EF6事务,所以现在它应该更像是代码优先模式。



此外,这是上下文类:

 公共类MyContext:BaseDbContext 
{
public MyContext(DbConnection existingConnection,bool contextOwnsConnection)
:base(existingConnection ,contextOwnsConnection)
{
}
}

字符串:

 <添加名称= CS 
connectionString = Data Source = MyServ; Initial Catalog = MyDBName ; User Id = MyUser; Password = MyPwd; Pooling = True; Min Pool Size = 5; Max Pool Size = 20; Persist Security Info = True; MultipleActiveResultSets = True; Application Name = EntityFramework; Enlist = false
providerName = System.Data.EntityClient />

我尝试将 providerName 设置为 System.Data.SqlClient ,但它没有任何改变。



请注意,原始连接字符串在数据库中第一种格式:

 <添加名称= OrderManagement 
connectionString = metadata = res:// * / MyManagementModel.csdl | res://*/MyManagementModel.ssdl | res://*/MyManagementModel.msl; provider = System.Data.SqlClient; provider连接字符串=& quot;数据源= MyServer;初始目录= MyDBName;用户ID = MyUser;密码= MyPwd;池=真;最小池大小= 5;最大池大小= 20;持久安全信息=真; MultipleActiveResultSets =真;应用程序名称= EntityFramework&
providerName = System.Data.EntityClient />

如果我尝试打开连接时将连接字符串保留为数据库第一格式,则我遇到异常不支持关键字元数据,并且当我将连接字符串放在代码优先格式上时,我遇到错误正在使用上下文在代码优先模式下,使用从EDMX文件为数据库优先或模型优先开发生成的代码。

解决方案

将上下文从数据库优先转换为代码优先时最常见的错误是忘记删除生成的 OnModelCreating 。在数据库优先的上下文中, OnModelCreating 引发异常:

 受保护重写void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException(); //←引发异常
}

因此,请查看您的上下文或其基类(及其子类),如果与上面的代码类似,则应将其删除。



您的代码和配置也需要进行一些更改。



您正在使用 providerName = System.Data.EntityClient 用于代码优先上下文。您应该将连接字符串更改为以下内容:

 < add name = MyContext connectionString = data source = server ; 
初始目录=数据库;用户ID =用户;密码=密码;
multipleactiveresultsets = True;应用程序名称= EntityFramework
providerName = System.Data.SqlClient />

然后,您应该将上下文构造函数更改为以下形式:

 公共部分类MyContext:DbContext 
{
public MyContext():base( name = MyContext){/ *。 。 。* /}
//。 。 。
}

此外,如果您想拥有一个通用的基类,请遵循以上说明。 / p>

注意:要从现有数据库中先创建代码,可以右键单击项目,然后选择添加新项 >,然后在数据下的 Visual C#下,选择 ADO.NET实体数据模型,然后单击添加。然后从实体数据模型向导中选择从数据库中获取代码优先并按照向导进行操作。有关更多信息,请参见首先将实体框架代码添加到现有数据库


I m trying to migrate a project initially developed using EF4 to EF6, to take advantage of EF6 transactions management.

The problem I m facing is that the project has been created using the Database First approach, so when I m using code like context.Database.UseTransaction, I m encountering the following error:

The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.

This exception triggers inside the OnModelCreating method of my DbContext class.

Any idea ?

Thanks

EDIT:

The thing is that it's legacy code using EDMX with database first approach. I have to implement EF6 transactions inside this project, so it should now be more like a code first pattern.

In addition, here's the context class:

public class MyContext : BaseDbContext
{
    public MyContext (DbConnection existingConnection, bool contextOwnsConnection)
        : base(existingConnection, contextOwnsConnection)
    {
    }
}

And the connection string:

  <add name="CS"
         connectionString="Data Source=MyServ;Initial Catalog=MyDBName;User Id=MyUser;Password=MyPwd;Pooling=True;Min Pool Size=5;Max Pool Size=20;Persist Security Info=True;MultipleActiveResultSets=True;Application Name=EntityFramework;Enlist=false"
         providerName="System.Data.EntityClient" />

I tried setting the providerName to System.Data.SqlClient but it doesn't change anything.

Please note that the original connection string was in the database first format:

<add name="OrderManagement"
     connectionString="metadata=res://*/MyManagementModel.csdl|res://*/MyManagementModel.ssdl|res://*/MyManagementModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MyServer;Initial Catalog=MyDBName;User Id=MyUser;Password=MyPwd;Pooling=True;Min Pool Size=5;Max Pool Size=20;Persist Security Info=True;MultipleActiveResultSets=True;Application Name=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

If I try to open a connection whilst I keep the connection string in the database first format, I m facing the exception Keyword metadata not supported, and when I put the connection string on the code first format, I m facing the error The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.

解决方案

The most common mistake when converting context from database-first to code first is forgetting to remove the generated OnModelCreating. In a database-first context, OnModelCreating throws the exception:

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

So take a look at your context or its base class (and their partial classes) and if it's like above code, you should remove it.

Also your code and configurations need some changes.

You are using providerName="System.Data.EntityClient" for a code first context. You should change the connection string to something like this:

<add name="MyContext" connectionString="data source=server;
initial catalog=database;User Id=user;Password=password;
multipleactiveresultsets=True;application name=EntityFramework" 
providerName="System.Data.SqlClient" />

Then you should change your context constructor to something like this:

public partial class MyContext: DbContext
{
    public MyContext() : base("name=MyContext") { /* . . .*/ }
    // . . . 
}

Also if you want to have a common base class, follow above instructions.

Note: To create code-first from an existing database, you can right click on project and choose Add New Item, then under Visual C# under Data select ADO.NET Entity Data Model and click on Add. Then from the Entity Data Model Wizard choose Code First from Database and follow the wizard. For more information see Entity Framework Code First to an Existing Database

这篇关于该上下文在Code First模式下与从EDMX文件生成的代码一起用于Database First或Model First开发。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆