在迁移到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;
}
}
答案 0 :(得分:1)
我最终在实体映射自身内部配置了.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);
});
}