【问题标题】:Conflicting configuration settings with IsNullable = true IsNullable = false reusing ComplexType与 IsNullable = true IsNullable = false 重用 ComplexType 冲突的配置设置
【发布时间】:2017-03-31 11:18:15
【问题描述】:

我不知道这种行为是设计使然,还是 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 命令失败并出现以下错误: 为“公司”类型的属性“名称”指定了冲突的配置设置: IsNullable = False 与 IsNullable = True 冲突

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

【问题讨论】:

  • HasColumnName() 定义属性将在数据库中定位的列。您正在尝试为相同的列定义不同的名称和可为空的规则。默认情况下,FK 应该是可空的,因此您需要使用 HasOne() 和 WithOne() 方法来实现不可空
  • @james,它们不是相同的列,因为在数据库中,可为空的列在 Customer 表中定义,不可为空的列在 AcademicInfo 表中。该问题不涉及 FK,因此我删除了与之相关的代码: modelBuilder.Entity() .HasRequired(a => a.Customer) .WithOptional(c => c.AcademicInfo) .WillCascadeOnDelete(true) ;

标签: c# entity-framework-6 entity-framework-migrations


【解决方案1】:

这是一个老问题,但对于 EF 版本 6.1.3 仍然有效。

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

这是 EF 的限制,一些属性方面需要存储在 C-Space 中,并且 EF 没有配置复杂类型的特定用法的方法。所以你只能指定不同的 S-Space facets,比如 ColumnName 或 ColumnType

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 2017-08-10
    • 2018-04-27
    • 2015-05-26
    • 1970-01-01
    • 2017-03-31
    • 2013-04-18
    • 2020-01-02
    相关资源
    最近更新 更多