The context is used in Code First mode with the code that was generated from the EDMX file for the First or Model First database.

I am trying to migrate a project originally developed using EF4 to EF6 in order to use EF6 transaction management.

The problem I am facing is that the project was created using the Database First approach, so when I use type code context.Database.UseTransaction, I encounter 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 is thrown inside the method of OnModelCreatingmy class DbContext.

Any idea?

thank

EDIT:

The fact is that this is legacy code using EDMX with a basic approach to the database. I have to implement EF6 transactions inside this project, so now it will be more like the first code template.

, :

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

:

  <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" />

providerName System.Data.SqlClient, .

, :

<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" />

, , Keyword metadata not supported, , 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.

+4
3

- OnModelCreating. OnModelCreating :

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

, ( ), , , .

.

providerName="System.Data.EntityClient" . - :

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

- :

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

, , .

: , , Visual # "" " ADO.NET" "". Entity Data Model Wizard Code First . . Entity Framework Code First to Existing Database.

+5

App.config Web.config.

, EDMX .

.

+1

web.config,

ConnectionString = "metadata = Res: ///Models.wimEntities.csdl | Res: ///Models.wimEntities.ssdl | Res: //*/Models.wimEntities.msl; provider = System.Data.SqlClient; provider connection string = "data source =; start directory = database; stored security information = True; MultipleActiveResultSets = True; App = EntityFramework "" ProviderName = "System.Data.EntityClient"

0
source

Source: https://habr.com/ru/post/1660817/


All Articles