实体类型“ModeratedUser”需要定义主键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实体类型“ModeratedUser”需要定义主键相关的知识,希望对你有一定的参考价值。

我使用代码优先数据库迁移遇到AspNetCore.Identity.EntityFrameworkCore 2.1.6的错误。错误是

实体类型“ModeratedUser”需要定义主键。

这让我感到困惑,因为我为使用Fluent Api的所有实体定义了主键。我已经对这个具体问题进行了研究,我遇到的所有帖子都提到了不同的问题(请注意我已经按照以下内容来定义我的多对多关系:https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration

以下是实体和实体类型配置:

public class ModeratedUser
{
    public Guid ModeratedId { get; set; }

    public virtual List<ModeratorModerated> ModeratorModerated { get; set; }
}

public class ModeratorUser
{
    public Guid ModeratorId { get; set; }

    public virtual List<ModeratorModerated> ModeratorModerated { get; set; }
}

//explicit class to outline many to many between moderated and moderators
public class ModeratorModerated
{
    public Guid ModeratorId { get; set; }
    public Guid ModeratedId { get; set; }
    public ModeratedUser Moderated { get; set; }
    public ModeratorUser Moderator { get; set; }
}

这是实体类型配置:

public abstract class ModeratedConfiguration : EntityMappingConfiguration<ModeratedUser>
{
    public override void MapToConfig(EntityTypeBuilder<ModeratedUser> builder)
    {
        builder.HasKey(x => x.ModeratedId);

        builder.ToTable("ModeratedUsers", "Mod");
    }
}

public abstract class ModeratorsConfiguration : EntityMappingConfiguration<ModeratorUser>
{
    public override void MapToConfig(EntityTypeBuilder<ModeratorUser> builder)
    {
        builder.HasKey(x => x.ModeratorId);
        builder.ToTable("ModeratorUsers", "Mod");
    }
}

public abstract class ModeratorModeratedConfiguration : EntityMappingConfiguration<ModeratorModerated>
{
    public override void MapToConfig(EntityTypeBuilder<ModeratorModerated> builder)
    {
        builder.HasKey(x => new { x.ModeratedId, x.ModeratorId });

        builder.HasOne(x => x.Moderated)
            .WithMany(x => x.ModeratorModerated)
            .HasForeignKey(x => x.ModeratedId);

        builder.HasOne(x => x.Moderator)
            .WithMany(x => x.ModeratorModerated)
            .HasForeignKey(x => x.ModeratorId);

        builder.ToTable("ModeratorModerated", "Mod");
    }
}

如您所见,我为ModeratorUser.cs和ModeratedUser.cs设置了.HasKey,并为ModeratorModerated.cs配置类设置了CompositeKey。

这是我的Context类:

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

    public DbSet<CatalogItem> CatalogItems { get; set; }
    public DbSet<ModeratedUser> ModeratedUsers { get; set; }
    public DbSet<ModeratorUser> ModeratorUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.HasDefaultSchema("UDC");

        builder.AddEntityConfigurationsFromAssembly(GetType().Assembly);

        base.OnModelCreating(builder);
    }
}

这是我用来实例化EntityMappingConfigurations的ModelBuilderExtensions.cs类...

public interface IEntityMappingConfiguration
{
    void MapToConfig(ModelBuilder b);
}

public interface IEntityMappingConfiguration<T> : IEntityMappingConfiguration where T : class
{
    void MapToConfig(EntityTypeBuilder<T> builder);
}

public abstract class EntityMappingConfiguration<T> : IEntityMappingConfiguration<T> where T : class
{
    public abstract void MapToConfig(EntityTypeBuilder<T> b);

    public void MapToConfig(ModelBuilder b)
    {
        MapToConfig(b.Entity<T>());
    }
}

public static class ModelBuilderExtenions
{
    private static IEnumerable<Type> GetMappingTypes(this Assembly assembly, Type mappingInterface)
    {
        return assembly.GetTypes().Where(x => !x.IsAbstract && x.GetInterfaces().Any(y => y.GetTypeInfo().IsGenericType && y.GetGenericTypeDefinition() == mappingInterface));
    }

    public static void AddEntityConfigurationsFromAssembly(this ModelBuilder modelBuilder, Assembly assembly)
    {
        var mappingTypes = assembly.GetMappingTypes(typeof(IEntityMappingConfiguration<>));
        foreach (var config in mappingTypes.Select(Activator.CreateInstance).Cast<IEntityMappingConfiguration>())
        {
            config.MapToConfig(modelBuilder);
        }
    }
}

来自VS2017中PackageManagerConsole窗口的错误是:

System.InvalidOperationException:实体类型“ModeratedUser”需要定义主键。

正如你所看到的,小应用程序,但我无法通过使用EFCore的第一个m-m。这在EF6.2中几乎没有那么具有挑战性。我在这里错过了什么?

答案

试试这个改变

public class ModeratedConfiguration : IEntityTypeConfiguration<ModeratedUser>
{
    public void Configure(EntityTypeBuilder<ModeratedUser> builder)
    {
        builder.ToTable("ModeratedUsers", "Mod");
        builder.HasKey(x => x.ModeratedId);
    }
}

public class ModeratorsConfiguration : IEntityTypeConfiguration<ModeratorUser>
{
    public void Configure(EntityTypeBuilder<ModeratorUser> builder)
    {
        builder.ToTable("ModeratorUsers", "Mod");
        builder.HasKey(x => x.ModeratorId);
    }
}

public class ModeratorModeratedConfiguration : IEntityTypeConfiguration<ModeratorModerated>
{
    public void Configure(EntityTypeBuilder<ModeratorModerated> builder)
    {
        builder.ToTable("ModeratorModerated", "Mod");
        builder.HasKey(x => new { x.ModeratedId, x.ModeratorId });

        builder.HasOne(x => x.Moderated)
            .WithMany(x => x.ModeratorModerated)
            .HasForeignKey(x => x.ModeratedId);

        builder.HasOne(x => x.Moderator)
            .WithMany(x => x.ModeratorModerated)
            .HasForeignKey(x => x.ModeratorId);

    }
}

你的DbContext应该是

public class DbContext : IdentityDbContext<ApplicationUser>
{

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

    public DbSet<CatalogItem> CatalogItems { get; set; }
    public DbSet<ModeratedUser> ModeratedUsers { get; set; }
    public DbSet<ModeratorUser> ModeratorUsers { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.HasDefaultSchema("UDC");

        builder.ApplyConfiguration<ModeratedUser>(new ModeratedUser());
        builder.ApplyConfiguration<ModeratedUser>(new ModeratorsConfiguration());
        builder.ApplyConfiguration<ModeratorModerated>(new ModeratorModeratedConfiguration());
        base.OnModelCreating(builder);
    }
}

以上是关于实体类型“ModeratedUser”需要定义主键的主要内容,如果未能解决你的问题,请参考以下文章

实体类型“X”需要定义主键

EF Core:实体类型“用户”需要定义主键

Entity Framework Core “实体类型‘XXX’需要定义一个主键。”

实体类型“IdentityUserLogin<string>”需要定义一个主键[重复]

ASP.NET Core Web API - 实体类型“IdentityUserRole<long>”需要定义主键

实体“学生”需要定义主键