C# 复合密钥问题
我需要一些帮助。我有两个解决方案需要给出相同的结果。其中一个行得通,但另一个不行C# 复合密钥问题,c#,entity-framework,entity-relationship,fluent,ef-database-first,C#,Entity Framework,Entity Relationship,Fluent,Ef Database First,我需要一些帮助。我有两个解决方案需要给出相同的结果。其中一个行得通,但另一个不行 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 Create
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 int ClacID{get;set;}
公共字符串CalcNumber{get;set;}
公共字符串BillNumber{get;set;}
公共日期时间BillDate{get;set;}
公共日期时间CreateDate{get;set;}
公共日期时间修改日期{get;set;}
公共布尔被删除{get;set;}
上市公司{get;set;}
public int SupplierID{get;set;}
public int CompanyID{get;set;}
}
公营公司
{
public int CompanyID{get;set;}
public int?注册号{get;set;}
公共字符串CompanyName{get;set;}
public CompanyRegister CompanyRegister{get;set;}
公共字符串OwnerFirstname{get;set;}
公共字符串OwnerLastname{get;set;}
公共字符串地址{get;set;}
公共场所{get;set;}
公共字符串Telefon{get;set;}
公共字符串传真{get;set;}
公共字符串Mobile{get;set;}
公共字符串电子邮件{get;set;}
public int?CompanyType{get;set;}
public int?UserId{get;set;}
公共用户用户{get;set;}
//参考资料
公共列表计算列表{get;set;}
上市公司()
{
地点=新地点();
CompanyRegister=新CompanyRegister();
CalculationList=新列表();
}
}
配置:
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");
}
}
公共类计算配置:EntityTypeConfiguration
{
公共计算配置()
{
//餐桌
可转帐(“计算”);
//初级的
HasKey(p=>p.ClacID);
//外键
HasRequired(c=>c.公司)
.WithMany(c=>c.CalculationList)
.HasForeignKey(d=>new{d.CompanyID,d.SupplierID});
//HasRequired(c=>c.Company)。WithMany(c=>c.CalculationList)。HasForeignKey(c=>c.CompanyID);
//HasRequired(c=>c.Company)。WithMany(c=>c.CalculationList)。HasForeignKey(c=>c.SupplierID);
//地图
属性(p=>p.ClacID);
属性(p=>p.CompanyID)。HasColumnName(“CompanyID”);
属性(p=>p.CalcNumber).HasColumnName(“CalcNumber”).HasMaxLength(10);
属性(p=>p.SupplierID).HasColumnName(“SupplierID”);
属性(p=>p.BillNumber).HasColumnName(“BillNumber”).HasMaxLength(100);
属性(p=>p.BillDate)。HasColumnName(“BillDate”);
属性(p=>p.CreateDate).HasColumnName(“CreateDate”);
属性(p=>p.ModifiedDate).HasColumnName(“ModifiedDate”);
属性(p=>p.IsDeleted);
}
}
不工作:
//Foreign Keys
HasRequired<Company>(c => c.Company)
.WithMany(c => c.CalculationList)
.HasForeignKey(d => new { d.CompanyID, d.SupplierID });
//外键
HasRequired(c=>c.公司)
.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);
HasRequired(c=>c.Company)。有许多(c=>c.CalculationList)。HasForeignKey(c=>c.CompanyID);
HasRequired(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 });
//外键
HasRequired(c=>c.公司)
.WithMany(c=>c.CalculationList)
.HasForeignKey(d=>新公司{d.CompanyID,d.SupplierID});
EF(高达6.1)仅接受设置主实体中的PK和从属实体中的FK之间的关系
您不能将FK指定给公司
,因为它依赖的不仅仅是公司ID
,而是公司
的主键
不能基于AK设置关系。但在示例代码中,它甚至不是AK(备用键=具有唯一约束的列集=候选键)
为什么不只将关系设置为PKCompanyId
?使用与PK或AK不同的东西是没有意义的
顺便说一句,如果它真的是AK,并且你想要这个功能,那么去投票支持它(这通常是最需要的功能)我已经编辑了你的标题。请不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标签就是为了这个目的。还有,如果一致意见是“不,他们不应该”,那么什么是“不起作用的”“意思是-你得到了什么错误/坏结果?嗨,当我想添加到数据库时,我得到了这个异常。EntityFramework.dll中发生类型为“System.Data.Entity.ModelConfiguration.ModelValidationException”的未处理异常。其他信息:在模型生成过程中检测到一个或多个验证错误:DataAccess.Logging::EntityType“Logging”未定义键。定义此EntityType.Calculation\u Company\u Target\u Calculation\u Company\u Source::关系约束中从属角色和主体角色中的属性数必须相同。Logging:EntityType:EntitySet“Logging”基于未定义键的类型“Logging”。感谢您的反馈。如果我理解正确的话。这只是我目前的解决方案:HasRequired(c=>c.Company)。WithMany(c=>c.CalculationList)。HasForeignKey(c=>c.CompanyID);HasRequired(c=>c.Company)。WithMany(c=>c.CalculationList)。HasForeignKey(c=>c.SupplierID);