Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core: Method 'ProcessModelFinalized' does not have implementation

I'm facing an error while trying to obtain information of the DbSets in a DbContext object by using Entity Framework core.

My DbContext object looks this way:

public class CatalogueContext : DbContext
{
    public DbSet<ConnectorCatalogueItemConv> CatalogueItemConvs { get; set; }

    public CatalogueContext(DbContextOptions<CatalogueContext> options)
        : base(options)
    {

    }

    public CatalogueContext()
    {

    }
}

I'm trying to instante the context by calling a method which receives a generic type T which might be childs of DbContext this way:

public T GetContext<T>() where T: DbContext, new()
{
    var optionsBuilder = new DbContextOptionsBuilder<T>();
    var connectionString = Configuration.GetConnectionString(ExternalTablesKey);
    optionsBuilder.UseSqlServer(connectionString);
    return Activator.CreateInstance(typeof(T), optionsBuilder.Options) as T;
}

The connection string is obtained properly by using Microsoft.Extensions.Configuration so the problem it's not there.

Finally, I invoke this method and try to obtain any record on the DbSets declared as follows:

 public void SomeMethod()
 {
     using (var db = this.GetContext<CatalogueContext>())
     {
         var val = db.CatalogueItemConvs.ToList(); //Here is where I get the error below.
     }
 }

The error shown says:

Method 'ProcessModelFinalized' in type 'Microsoft.EntityFrameworkCore.Metadata.Conventions.SqlServerValueGenerationStrategyConvention' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.'

I searched everywhere, but seems to be that there's very few information about this error. Any thoughts?

EDIT 1: My solution includes Microsoft.EntityFrameworkCore.SqlServer in version 3.0.0.0

EDIT 2: As requested, I have edited the code to include only one entity and its class declaration. There's no mapping at this point.

public class ConnectorCatalogueItemConv
{
    public string CodConnector { get; set; }
    public string CodCatalogue { get; set; }
    public string CodItemCia { get; set; }
    public string CodItemInbk { get; set; }
    public bool Defaultvalue { get; set; }
}

EDIT 3: How to instantiate a DbContext in EF Core

like image 528
Mauro Bilotti Avatar asked Mar 20 '20 19:03

Mauro Bilotti


People also ask

What is the difference between EF6 and EF core?

Guidance for existing EF6 applicationsKeep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Can I use EF core in .NET framework?

You can use EF Core in APIs and applications that require the full . NET Framework, as well as those that target only the cross-platform .

Is EF core ORM?

Entity Framework Core is what's known as an Object Relational Mapper (ORM). Created by Microsoft, the library allows developers to work abstractly with their database.

How does EF core work?

EF Core can detect when you change a property in a class you read in from the database. It does this by holding a hidden copy of the class(es) read in. When you call SaveChanges it compares each clear read in with its original value and only creates commands to change the specific class/property that was changed.


1 Answers

I have also faced the same error, I have update my Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools to Version 5.0 and then it worked,

like image 79
AXz Avatar answered Nov 17 '22 16:11

AXz