0

我需要一些帮助。我有两个解决方案需要给出相同的结果。其中一个工作很好,但另一个工作没有成功。复合键问题

public class Calculation 
{ 
    public int ClacID { get; set; } 
    public string CalcNumber { get; set; } 
    public string BillNumber { get; set; } 
    public DateTime BillDate { get; set; } 
    public DateTime CreateDate { get; set; } 
    public DateTime ModifiedDate { get; set; } 
    public bool IsDeleted { get; set; } 

    public Company Company { get; set; } 
    public int SupplierID { get; set; } 
    public int CompanyID { get; set; } 
} 

public class Company 
{ 
    public int CompanyID { get; set; } 
    public int? RegistrationNumber { get; set; } 
    public string CompanyName { get; set; } 
    public CompanyRegister CompanyRegister { get; set; } 
    public string OwnerFirstname { get; set; } 
    public string OwnerLastname { get; set; } 
    public string Address { get; set; } 
    public Place Place { get; set; } 
    public string Telefon { get; set; } 
    public string Telefax { get; set; } 
    public string Mobile { get; set; } 
    public string Email { get; set; } 
    public int? CompanyType { get; set; } 

    public int? UserId { get; set; } 
    public Users User { get; set; } 

    //References 
    public List<Calculation> CalculationList { get; set; } 

    public Company() 
    { 
     Place = new Place(); 
     CompanyRegister = new CompanyRegister(); 
     CalculationList = new List<Calculation>(); 
    } 

} 

配置:

public class CalculationConfiguration : EntityTypeConfiguration<Calculation> 
    { 
     public CalculationConfiguration() 
     { 
      // Set Table 
      ToTable("Calculation"); 

      // Primary 
      HasKey(p => p.ClacID); 

      //Foreign Keys 
      HasRequired<Company>(c => c.Company) 
       .WithMany(c => c.CalculationList) 
       .HasForeignKey(d => new { d.CompanyID, d.SupplierID }); 

      //HasRequired<Company>(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.CompanyID); 
      //HasRequired<Company>(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.SupplierID); 

      //Map 
      Property(p => p.ClacID).HasColumnName("CalcID"); 
      Property(p => p.CompanyID).HasColumnName("CompanyID"); 
      Property(p => p.CalcNumber).HasColumnName("CalcNumber").HasMaxLength(10); 
      Property(p => p.SupplierID).HasColumnName("SupplierID"); 
      Property(p => p.BillNumber).HasColumnName("BillNumber").HasMaxLength(100); 
      Property(p => p.BillDate).HasColumnName("BillDate"); 
      Property(p => p.CreateDate).HasColumnName("CreateDate"); 
      Property(p => p.ModifiedDate).HasColumnName("ModifiedDate"); 
      Property(p => p.IsDeleted).HasColumnName("IsDeleted"); 
     } 
    } 

不起作用:

 //Foreign Keys 
     HasRequired<Company>(c => c.Company) 
      .WithMany(c => c.CalculationList) 
      .HasForeignKey(d => new { d.CompanyID, d.SupplierID }); 

工作:

HasRequired<Company>(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.CompanyID); 
HasRequired<Company>(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.SupplierID); 

有人可以在世界告诉我。怎么了 ?

谢谢。 Zlaja

+1

我编辑了自己的冠军。请不要包含有关问题标题中使用的语言的信息,除非在没有它的情况下没有意义。标签用于此目的。另请参阅[“应该在其标题中包含”标签“的问题?”](http://meta.stackoverflow.com/q/19190/193440),其中的共识是“不,他们不应该 – chridam

+0

什么” 't work'意思是 - 你得到了什么错误/不好的结果? –

+0

嗨,我想要添加到db的异常 在EntityFramework中发生了一个'System.Data.Entity.ModelConfiguration.ModelValidationException'类型的未处理异常。 dll的 其他信息:模型生成期间检测到一个或多个验证错误: DataAccess.Logging:的EntityType“记录”不具有确定的键定义此的EntityType关键 – user3748491

回答

1

试试下面的代码: -

//Foreign Keys 
     HasRequired<Company>(c => c.Company) 
      .WithMany(c => c.CalculationList) 
      .HasForeignKey(d => new Company { d.CompanyID, d.SupplierID }); 
+0

对不起,没有工作。我试过你的解决方案。 – user3748491

0

EF(高达6.1)只接受设置PK之间的关系在相关实体的主要实体和FK。

您无法指定FK为Company,因为它取决于CompanyId以上的值,其中的值为Company的PK值。

您不能根据AK设置关系。但是在你的示例代码中,它甚至不是一个AK(备用密钥=具有唯一约束=备选密钥的一组列)。

为什么不把关系只设置到PK CompanyId?使用与PK或AK不同的东西是没有意义的。

顺便说一句,如果它是真正的AK,和你想要的功能,去EF user's voice site,并为它投票(这是usualy最抢手的功能)

+0

感谢您的反馈。如果我正确理解你的话。这是我唯一的解决方案: HasRequired (c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.CompanyID); HasRequired (c => c.Company).WithMany(c => c.CalculationList)。HasForeignKey(c => c.SupplierID); – user3748491