.. Ignore()迁移到EFCore 3.1后引发异常

问题描述 投票:0回答:1

我在迁移到EFcore 3.1(迁移之前使用2.2之前,现在已经运行了这段代码,现在抛出了以下错误:'The type 'ProfileEnum' cannot be configured as non-owned because an owned entity type with the same name already exists.'

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
     modelBuilder.ApplyConfiguration(new UserConfig());

     modelBuilder.Entity<ProfileEnum>()
            .Ignore(p => p.Name);
}

场景是:ProfileEnum是一种复杂的类型,我使用以下代码块映射到User类:>

public class UserConfig : IEntityTypeConfiguration<User>
{
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.HasKey(x => x.UserId);

        builder.Property(x => x.Name)
            .HasMaxLength(200);

        builder.Property(x => x.DocumentNumber)
            .HasMaxLength(50);

        **builder.OwnsOne(x => x.Profile, profile =>
        {
            profile.Property(c => c.Value)
            .IsRequired()
            .HasColumnName("ProfileId")
            .HasColumnType("integer");
        });**
     }
 }


public class ProfileEnum
{
    public static ProfileEnum CompanyAdmin = new ProfileEnum(1, "CompanyAdmin");
    public static ProfileEnum Admin { get; } = new ProfileEnum(2, "Admin");
    public static ProfileEnum PowerUser { get; } = new ProfileEnum(3, "PowerUser");
    public static ProfileEnum Standard { get; } = new ProfileEnum(4, "Standard");
    private ProfileEnum(int val, string name)
    {
        Value = val;
        Name = name;
    }
}
    

我在迁移到EFcore 3.1(迁移之前使用2.2之前,现在已经使用了这段代码,现在抛出了以下错误:'类型'ProfileEnum'无法配置为非所有者...

c# ef-code-first entity ef-core-2.2 ef-core-3.1
1个回答
1
投票
我最终在实体映射自身内部配置了.ignore(p => p.Name),问题消失了
© www.soinside.com 2019 - 2024. All rights reserved.