7

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

Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
  • Please sow the entity classes and custom mapping code (if any). – Gert Arnold Mar 20 '20 at 20:05
  • I added a new edit with your request. As I say, there's no mapping. At this point, I only want to retrieve the content of a table on the DbSet. Thanks in advance. – Mauro Bilotti Mar 20 '20 at 20:10
  • I wonder why EF doesn't complain about `ConnectorCatalogueItemConv` not having a key property. – Gert Arnold Mar 20 '20 at 20:25
  • At least until the point where I marked the exception EF is not complaining about anything. Seems pretty fine until I reach that point. – Mauro Bilotti Mar 20 '20 at 20:26
  • 2
    Well, this all indicates that you haven't got a coherent collection of NuGet packages. Try to (re)install the latest `Microsoft.EntityFrameworkCore.SqlServer` package. – Gert Arnold Mar 20 '20 at 20:52
  • Thanks Gert! The solution comes with your contribution too. The that @suchi exposed solved the problem, but thanks to you also. – Mauro Bilotti Mar 21 '20 at 19:48

4 Answers4

17

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,

AXz
  • 316
  • 3
  • 15
  • 1
    Excellent answer! This has solved my problem. Thanks a lot, I have marked as the correct answer. – Mauro Bilotti Mar 21 '20 at 19:47
  • Something wierd happening when I'm going to update "Microsoft.EntityFrameworkCore.SqlServer" from 3.1.10 to 5.0. Can not update that? – Ali reza Soleimani Asl Nov 22 '20 at 06:03
  • 1
    By using this [tutorial](https://learn.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-5.0&tabs=visual-studio) I changed "TargetFramework" from "netcoreapp3.1" to "net5.0" and packets updated smoothly and the project builded successfully. – Ali reza Soleimani Asl Nov 22 '20 at 06:07
  • 2
    I encountered this error when installing a 5.0 version of a `Microsoft.EntityFrameworkCore.*` package in a test project that already had `Microsoft.EntityFrameworkCore.InMemory` installed a a 3.x version. This resolved the issue for me. – derekbaker783 Mar 08 '21 at 19:52
5

My two cents: If you wish to use the .NET core 3.0 version instead of upgrading to the .Net 5.0, then all you need to do is make sure that all the packages mentioned above use the version 3.x.x instead of the 5.x.x. I noticed that when you try to scaffold identity pages, it installs the 5.x.x version of the package since its the latest, instead of checking the package .net core version and installing the appropriate version.

goodcat
  • 200
  • 2
  • 9
  • This is actually the correct answer, if you are using EntityFrameworkCore make sure all of your related packages are on the same version. That's what the error message means, it can't find an implementation of the correct version of that method. So downgrading is not the way – Bunnynut Aug 27 '21 at 07:01
0

If you have same issue but in testing project and your are using Microsoft.EntityFrameworkCore.InMemory you fix this error by installing version v3.1.10

Fernando Torres
  • 1,070
  • 8
  • 19
0

I was using Microsoft.EntityFrameworkCore.InMemory v3.1.10 with Xunit v2.4.1 for testing and had the following error pop up while seeding a context:

System.TypeLoadException: 'Method 'CommitTransactionAsync' in type 'Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTransactionManager' from assembly 'Microsoft.EntityFrameworkCore.InMemory, Version=3.1.10.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.'

Updating all my Microsoft.EntityFrameworkCore packages to v5.0.0 cleared up the issue.

DMendoza
  • 412
  • 4
  • 8
  • If you get this error when referencing Microsoft.EntityFrameworkCore.InMemory then you may also want to check related packages such as MockQueryable.Moq. Basically they need to be in sync - either use 3.1.x packages in your project or all 5.x but not a mix of both. – Stuart Mar 08 '22 at 11:45