.Ignore () выбрасывает исключение после перехода на EFCore 3.1 - PullRequest
0 голосов
/ 07 марта 2020

У меня был этот блок кода, работающий до перехода на 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;
    }
}

1 Ответ

1 голос
/ 09 марта 2020

Я закончил настройку .ignore(p => p.Name) внутри сопоставления сущности, и проблема исчезла

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");

        profile.Ignore(p => p.Name);
    });
 }
...