加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 与IsNullable冲突的配置设置= true IsNullable = false重

发布时间:2020-12-15 21:03:16 所属栏目:百科 来源:网络整理
导读:我不知道这种行为是设计的还是EF6中的错误,或者还有另一种方法可以做到这一点.有这种复杂的类型: [ComplexType]public partial class Company public bool HasValue { get { return !string.IsNullOrEmpty(this.Name); } } [MaxLength(100)] public string
我不知道这种行为是设计的还是EF6中的错误,或者还有另一种方法可以做到这一点.有这种复杂的类型:

[ComplexType]
public partial class Company    
    public bool HasValue { get { return !string.IsNullOrEmpty(this.Name); } }

    [MaxLength(100)]
    public string Name { get; set; }

    [MaxLength(20)]
    public string PhoneNumber { get; set; }

    [MaxLength(128)]
    public string EmailAddress { get; set; }
}

我在这两个实体中重用它:

public partial class Customer
{
    public Customer ()
    {
        this.Company = new Company();       
    }

    [Key]
    public int IdCustomer { get; set; }

    [MaxLength(100)]
    [Required]
    public string FirstName { get; set; }

    [MaxLength(100)]
    [Required]
    public string LastName { get; set; }

    public Company Company { get; set; }

    public virtual AcademicInfo AcademicInfo { get; set; }
}


public partial class AcademicInfo
{       
    public AcademicInfo()
    {
        this.Organization = new Company();
    }       

    [Key,ForeignKey("Customer")]
    public int IdCustomer { get; set; }

    public Company Organization { get; set; }

    [MaxLength(100)]
    public string Subject { get; set; }

    [MaxLength(100)]
    public string Degree { get; set; }

    public virtual Customer Customer { get; set; }
}

在dbcontext的OnModelCreating中(编辑:为了简单起见,我添加了之前省略的FK代码):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{    
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    // ... Other code here related to entities not related to the problem reported omitted to avoid confusion.

    modelBuilder.Entity<AcademicInfo>()
            .HasRequired(a => a.Customer)
            .WithOptional(c => c.AcademicInfo)
            .WillCascadeOnDelete(true);

    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.Name)
            .HasColumnName("CompanyName")
            .IsOptional(); // CONFLICT HERE
    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.EmailAddress)
            .HasColumnName("CompanyEmailAddress")
            .IsOptional();  //CONFLICT HERE
    modelBuilder.Entity<Customer>()
            .Property(p => p.Company.PhoneNumber)
            .HasColumnName("CompanyPhoneNumber")
            .IsOptional();

    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.Name)
            .HasColumnName("OrganizationName")
            .IsRequired(); // CONFLICT
    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.EmailAddress)
            .HasColumnName("OrganizationEmail")
            .IsRequired(); // CONFLICT
    modelBuilder.Entity<AcademicInfo>()
            .Property(a => a.Organization.PhoneNumber)
            .HasColumnName("OrganizationPhone")
            .IsOptional();
}

Add-Migration命令失败,并显示以下错误:
为’Company’类型的属性’Name’指定了冲突的配置设置:
????IsNullable =与IsNullable = True的错误冲突

但它没有任何意义,因为我在AcademicInfo表中定义了不可为空的字段,在Customer表中定义了可为空的字段.

解决方法

这是一个老问题,但仍然适用于EF版本6.1.3.

根据this issue,行为是关于如何配置特定复杂类型的实体框架限制.

This is a limitation of EF,some property facets need to be stored in C-Space and EF doesn’t have a way of configuring a particular usage of a complex type. So you can only specify different S-Space facets like ColumnName or ColumnType

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读