[英]Using Entity Framework 4.1, how do I configure one to many relationship with composite keys using different field names?
I have two tables: DeptMaster and LaborMaster where one DeptMaster has many LaborMasters. 我有两个表:DeptMaster和LaborMaster,其中一个DeptMaster有许多LaborMaster。 These are legacy tables from an old database so they don't follow Code First naming conventions.
这些是旧数据库中的旧表,因此它们不遵循Code First命名约定。 They both use composite keys but with different column names in each table.
它们都使用复合键,但每个表中都有不同的列名。 The classes have navigational properties as well.
这些类也具有导航属性。 I would prefer mapping them with attributes but please show me that method if possible but also the fluent way.
我更喜欢用属性映射它们,但如果可能的话,请告诉我这种方法,但也要流畅的方式。
[Table("DeptMaster"), Schema="HR"]
public partial class DeptMaster
{
public DeptMaster()
{
this.LaborMasters = new HashSet<LaborMaster>();
}
[Key, Column(Order = 0)]
public decimal DCompanyNum {get;set;}
[Key, Column(Order = 1)]
public decimal DDivisionNum {get;set;}
[Key, Column(Order = 2)]
public decimal DDeptNum {get;set;}
public string DDeptName {get;set;}
public virtual ICollection<LaborMaster> LaborMasters { get; set; }
}
[Table("LaborMaster"), Schema="HR"]
public partial class LaborMaster
{
[Key, Column(Order = 0)]
public decimal LCompanyNum {get;set;}
[Key, Column(Order = 1)]
public decimal LDivisionNum {get;set;}
[Key, Column(Order = 2)]
public decimal LDeptNum {get;set;}
[Key, Column(Order = 3)]
public decimal LEmployeeNum {get; set;}
public string LLaborName {get;set;}
public virtual DeptMaster DeptMaster{ get; set; }
}
In my OnModelCreating, I attempted to use the HasMany but it is not clear how I will make Entity Framework know how LCompanyNum, LDivisionNum, and LDeptNum of the child points back to DCompanyNum, DDivisionNum, and DDeptNum of the parent when the fields do not match by name. 在我的OnModelCreating中,我尝试使用HasMany,但是我不知道如何让实体框架知道当字段不匹配时,子组件的LCompanyNum,LDivisionNum和LDeptNum如何指向父组件的DCompanyNum,DDivisionNum和DDeptNum按名字。 Again, I would prefer using attributes but I'm not sure if it's possible.
同样,我更喜欢使用属性,但我不确定它是否可行。 I'm open to either.
我愿意接受。 Thank You.
谢谢。
You just need to add the [ForeignKey]
attribute for the first three properties: 您只需要为前三个属性添加
[ForeignKey]
属性:
[Table("LaborMaster"), Schema="HR"]
public partial class LaborMaster
{
[Key, ForeignKey("DeptMaster"), Column(Order = 0)]
public decimal LCompanyNum {get;set;}
[Key, ForeignKey("DeptMaster"), Column(Order = 1)]
public decimal LDivisionNum {get;set;}
[Key, ForeignKey("DeptMaster"), Column(Order = 2)]
public decimal LDeptNum {get;set;}
[Key, Column(Order = 3)]
public decimal LEmployeeNum {get; set;}
public string LLaborName {get;set;}
public virtual DeptMaster DeptMaster{ get; set; }
}
Or alternatively put the [ForeignKey]
attribute on the navigation property: 或者将
[ForeignKey]
属性放在导航属性上:
[ForeignKey("LCompanyNum, LDivisionNum, LDeptNum")]
public virtual DeptMaster DeptMaster{ get; set; }
Here the order of the property names matters, it must follow the column order. 这里属性名称的顺序很重要,它必须遵循列顺序。
With Fluent API: 使用Fluent API:
modelBuilder.Entity<DeptMaster>()
.HasKey(d => new { d.DCompanyNum, d.DDivisionNum, d.DDeptNum })
.HasMany(d => d.LaborMasters)
.WithRequired(l => l.DeptMaster)
.HasForeignKey(l => new { l.LCompanyNum, l.LDivisionNum, l.LDeptNum });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.