gpt4 book ai didi

c# - 与 IsNullable 冲突的配置设置 = true IsNullable = false 重用 ComplexType

转载 作者:行者123 更新时间:2023-11-30 12:56:32 26 4
gpt4 key购买 nike

我不知道此行为是设计使然还是 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();
}

添加迁移命令失败并出现以下错误:为“公司”类型的属性“名称”指定了冲突的配置设置: IsNullable = False 与 IsNullable = True 冲突

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

最佳答案

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

根据 this issue该行为是关于如何配置特定复杂类型的 Entity Framework 限制。

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

关于c# - 与 IsNullable 冲突的配置设置 = true IsNullable = false 重用 ComplexType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40646017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com