【问题标题】:Composite keys issue复合键问题
【发布时间】:2014-06-17 12:52:27
【问题描述】:

我需要一些帮助。我有两个解决方案需要给出相同的结果。其中一个工作正常,但另一个不工作。

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

世界上有谁能告诉我。怎么了 ?

谢谢。 兹拉亚

【问题讨论】:

  • 我已经编辑了你的标题。请不要包含有关问题标题中使用的语言的信息,除非没有它就没有意义。标签用于此目的。另请参阅"Should questions include “tags” in their titles?",其中的共识是“不,他们不应该
  • “不起作用”是什么意思 - 你得到什么错误/坏结果?
  • 嗨,当我想添加到数据库时,我得到了这个异常。 EntityFramework.dll 中出现“System.Data.Entity.ModelConfiguration.ModelValidationException”类型的未处理异常附加信息:在模型生成过程中检测到一个或多个验证错误:DataAccess.Logging: : EntityType 'Logging' 没有定义键。定义此 EntityType 的键。
  • Calculation_Company_Target_Calculation_Company_Source: :关系约束中的从属角色和主要角色中的属性数量必须相同。日志记录:EntityType:EntitySet 'Logging' 基于没有定义键的类型 'Logging'。

标签: c# entity-framework entity-relationship fluent ef-database-first


【解决方案1】:

试试下面的代码:-

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

【讨论】:

    【解决方案2】:

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

    您不能将 FK 指定为 Company,因为它依赖于 CompanyId 以外的其他内容,其中是 Company 的 PK。

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

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

    顺便说一句,如果它真的是 AK,并且您想要该功能,请转到 EF user's voice site,并为它投票(通常是最需要的功能)

    【讨论】:

    • 感谢您的反馈。如果我理解正确的话。这只是我暂时的解决方案: HasRequired(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.CompanyID); HasRequired(c => c.Company).WithMany(c => c.CalculationList).HasForeignKey(c => c.SupplierID);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多