2016-11-17 2 views
4

피드백 보내기 현재 상태이 동작은 의도적으로 또는 EF6 버그 인 경우 또는이 작업을 수행 할 수있는 다른 방법이 있습니다. dbcontext의 OnModelCreating에서IsNullable = true로 충돌하는 구성 설정 IsNullable = false ComplexType 재사용

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; } 
} 

:

[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; } 
} 

나는이 두 기관에서 재사용이 복합 형 갖는 (편집 : 나는 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 명령이 다음 오류와 함께 실패합니다. 유형의 'Name'속성에 대해 충돌하는 구성 설정이 지정되었습니다. '회사' ISNULLABLE = 거짓

진정한 ISNULLABLE =와 충돌하지만 내가 고객 테이블에서 AcademicInfo 테이블에 널 (NULL)와 널 (NULL)이 아닌 필드를 정의하기 때문에 아무 의미가 없습니다.

+0

HasColumnName()은 데이터베이스에서 속성이 대상으로하는 열을 정의합니다. 동일한 컬럼에 대해 서로 다른 이름과 널 (null) 입력 가능 규칙을 정의하려고합니다. 기본적으로 FK는 nullable이므로 null이 아닌 – monica

+0

@james에 대해 HasOne() 및 WithOne() 메서드를 사용해야하므로 데이터베이스에서 nullable 열이 Customer 테이블에 정의되어 있기 때문에 동일한 열이 아닙니다. Null 허용되지 않는 항목은 AcademicInfo 테이블에 있습니다. ( modelBuilder.Entity () \t \t \t \t .HasRequired (A => a.Customer) \t \t \t \t .WithOptional : 문제에 관련된 더 FK 그래서 난 그와 관련된 코드를 제거되지 있습니다 c => c.AcademicInfo) \t \t \t \t .WillCascadeOnDelete (true); – Lupa

답변

0

이것은 오래된 질문이지만 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