簡體   English   中英

.Ignore() 在遷移到 EFCore 3.1 后拋出異常

[英].Ignore() throwing exception after migration to EFCore 3.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;
    }
}

我最終在實體映射本身中配置了.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);
    });
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM