我不知道这种行为是设计的还是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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。