组合键问题



我需要一些帮助。我有两个解需要得到相同的结果。其中一个可以工作,但另一个不行。

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

尝试下面的代码:-

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

EF(直到6.1)只接受设置主实体中的PK和从属实体中的FK之间的关系。

你不能指定FK到Company,因为它依赖于CompanyId以外的东西,是Company的PK。

不能基于AK设置关系。但是在您的示例代码中,它甚至不是AK(备用键=具有唯一约束的列集=候选键)。

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

顺便说一下,如果它真的是一个AK,你想要这个功能,去EF用户的语音网站,投票支持它(它通常是最需要的功能)

相关内容

  • 没有找到相关文章

最新更新