Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 654
Podelo Avatar asked Nov 15 '16 16:11

Podelo


People also ask

Which data model should be used for code first approach?

For those developers, Entity Framework has a modeling workflow referred to as Code First. Code First modeling workflow targets a database that doesn't exist and Code First will create it. It can also be used if you have an empty database and then Code First will add new tables to it.

Why do we use code first approach?

With code first your hand coded models become your database. The exact files that you're building are what generate the database design. There are no additional files and there is no need to create a class extension when you want to add properties or whatever else that the database doesn't need to know about.


1 Answers

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

like image 150
Reza Aghaei Avatar answered Oct 19 '22 09:10

Reza Aghaei