Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Ignore()在迁移到EFCore 3.1后引发异常_C#_Ef Code First_Entity_Ef Core 2.2_Ef Core 3.1 - Fatal编程技术网

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

C# .Ignore()在迁移到EFCore 3.1后引发异常,c#,ef-code-first,entity,ef-core-2.2,ef-core-3.1,C#,Ef Code First,Entity,Ef Core 2.2,Ef Core 3.1,我在迁移到EFcore 3.1之前使用了这段代码(在迁移之前使用2.2),现在它抛出了以下异常:“不能将类型“ProfileEnum”配置为非自有,因为已经存在同名的自有实体类型。” 模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder) { ApplyConfiguration(newuserconfig()); modelBuilder.Entity() .Ignore(p=>p.Name); } 场景是:ProfileEnum是一种复杂类型,我使用以下块将其映

我在迁移到EFcore 3.1之前使用了这段代码(在迁移之前使用2.2),现在它抛出了以下异常:
“不能将类型“ProfileEnum”配置为非自有,因为已经存在同名的自有实体类型。”

模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
ApplyConfiguration(newuserconfig());
modelBuilder.Entity()
.Ignore(p=>p.Name);
}
场景是:ProfileEnum是一种复杂类型,我使用以下块将其映射到用户类

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;
    }
}
public类UserConfig:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
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(“整数”);
});**
}
}
公共类概要文件枚举
{
public static ProfileEnum CompanyAdmin=新ProfileEnum(1,“CompanyAdmin”);
publicstaticprofileenumadmin{get;}=newprofileenum(2,“Admin”);
publicstaticprofileenum PowerUser{get;}=newprofileenum(3,“PowerUser”);
publicstaticprofileenum标准{get;}=newprofileenum(4,“标准”);
私有配置文件枚举(int val,字符串名称)
{
值=val;
名称=名称;
}
}

我最终在实体映射本身内部配置了
.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);
    });
 }
public void配置(EntityTypeBuilder)
{
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(“整数”);
profile.Ignore(p=>p.Name);
});
}

配置文件枚举类型不完整,缺少属性。但我不清楚你将如何使用这种类型。如果EF不能设置
名称
,并且名称/值组合似乎是固定的,为什么会有包含
名称
的构造函数?为什么不使用常规枚举?顺便说一句,该错误是由将类型注册为实体的
modelBuilder.Entity()
引起的。
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);
    });
 }