Conflict configuration settings using IsNullable = true IsNullable = false reuse of ComplexType

I don't know if this is by design, or is it a bug in EF6, or is there another way to do this. Having this complex type:

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

I reuse it in these two objects:

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

in dbcontext OnModelCreating (EDIT: I added the FK code that I skipped earlier for simplicity):

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

The Add-Migration command fails: Conflicting configuration parameters were specified for the Name property in the Company type: IsNullable = False conflicts with IsNullable = True

But that doesn't make sense, because I defined fields that are not null in the AcademicInfo table and are NULL in the Customer table.

+4
1

, EF 6.1.3.

Entity Framework , .

EF, C-Space, EF . , S-, ColumnName ColumnType

0

Source: https://habr.com/ru/post/1661013/


All Articles