'IMutableEntityType' does not contain a definition for 'Cosmos' and no accessible extension method 'Cosmos' accepting a first argument

c# ef-core-3.1 entity-framework-core

Question

While migrating a class library project from .netcore2.2 to .netcore 3.1, I am getting the following error:

Error CS1061 'IMutableEntityType' does not contain a definition for 'Cosmos' and no accessible extension method 'Cosmos' accepting a first argument of type 'IMutableEntityType' could be found (are you missing a using directive or an assembly reference?)

In the .netcore2.2 project I have used the following nuget packages:

  1. AutoMapper.Extensions.Microsoft.DependencyInjection
  2. Microsoft.EntityFrameworkCore
  3. Microsoft.EntityFrameworkCore.Cosmos
  4. Microsoft.Extensions.Configuration

Now as part of the migration process I have updated all the above nuget packages to their latest versions.

enter image description here

Her goes my DbContext class:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options): base(options)
    {
    }

    protected MyDbContext()
    {
    }

    public DbSet<Address> Address { get; set;}

    public DbSet<Languages> Languages { get; set;}

    public DbSet<Contacts> Contacts { get; set;}


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        OneCollectionPerDbSet(modelBuilder);
    }

    private void OneCollectionPerDbSet(ModelBuilder modelBuilder)
    {
        var dbSets = typeof(MyDbContext).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.PropertyType.IsGenericType && typeof(DbSet<>).IsAssignableFrom(p.PropertyType.GetGenericTypeDefinition()));
        foreach (var dbSet in dbSets)
        {
            var metadata = modelBuilder.Entity(dbSet.PropertyType.GetGenericArguments()[0]).Metadata;
            metadata.Cosmos().ContainerName = dbSet.Name;
        }
    }
}

In the above code, I am getting error in the method: OneCollectionPerDbSet as shown in the below line:

metadata.Cosmos().ContainerName = dbSet.Name;

Can anyone help me to fix this issue by providing their guidance

1
1
12/24/2019 4:36:08 AM

Accepted Answer

In EF Core 3.0+ provider extension methods like Cosmos(), SqlServer() etc. as well as Relational() have been removed. Now they are provided as direct extension methods of the corresponding metadata interfaces, with all previous properties replaced with Get / Set extension methods.

In your case, the replacement of Cosmos().ContainerName property are GetContainer and SetContainer extension methods:

metadata.SetContainer(dbSet.Name);
2
12/23/2019 10:03:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow