c# - 'IMutableEntityType' 不包含 'Cosmos' 的定义,并且没有可访问的扩展方法 'Cosmos' 接受第一个参数

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

将类库项目从 .netcore2.2 迁移到 .netcore 3.1 时,出现以下错误:

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?)



.netcore2.2 项目我使用了以下 nuget 包:
  • AutoMapper.Extensions.Microsoft.DependencyInjection
  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Cosmos
  • Microsoft.Extensions.Configuration

  • 现在,作为迁移过程的一部分,我已将上述所有 nuget 包更新为最新版本。

    enter image description here

    她去了我的 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;
            }
        }
    }
    

    在上面的代码中,我在方法中遇到错误: OneCollectionPerDbSet 如下行所示:
    metadata.Cosmos().ContainerName = dbSet.Name;
    

    任何人都可以通过提供他们的指导来帮助我解决这个问题

    最佳答案

    在 EF Core 3.0+ 提供程序扩展方法中,如 Cosmos() , SqlServer()等等以及Relational()已被删除。现在它们作为相应元数据接口(interface)的直接扩展方法提供,所有以前的属性都替换为 Get/Set扩展方法。

    在您的情况下,替换 Cosmos().ContainerName属性是 GetContainerSetContainer扩展方法:

    metadata.SetContainer(dbSet.Name);
    

    关于c# - 'IMutableEntityType' 不包含 'Cosmos' 的定义,并且没有可访问的扩展方法 'Cosmos' 接受第一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59461194/

    相关文章:

    c# - 如何仅包含相关实体的选定属性

    c# - 当我使用 ToListAsync 方法时,线程不起作用

    c# - 使用 。在使用 LuaInterface 注册的方法中

    c# - 希伯来语和符号的正则表达式

    c# - EntityFrameworkCore 中的 IDbAsyncQueryProvider

    c# - 错误 OnModelCreating (ModelBuilder)' : no suitable method found to override

    c# - 减少.NET Core中常见列表属性比较的数量

    entity-framework - 如何建立一对多关系的中间表?

    c# - 当所有玩家同名时查找 localPlayer

    c# - 也许 monad 使用表达式树?