Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 271
santosh kumar patro Avatar asked Dec 23 '19 20:12

santosh kumar patro


1 Answers

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);
like image 103
Ivan Stoev Avatar answered Sep 27 '22 21:09

Ivan Stoev