使用实体框架4.1,我该如何配置一个使用不同的字段名称组合键一对多的关系?我该、字段、实体、框架

2023-09-07 09:13:45 作者:宇宙限量版帅哥

我有两个表:DeptMaster和LaborMaster其中一个DeptMaster有很多LaborMasters。这些都是从旧的数据库,使他们不遵守code首先命名约定原有表。它们都使用组合键,但在每个表中不同的列名。该班有导航性能以及。我想preFER带属性映射他们,但请告诉我这种方法,如果可能的,而且流畅的方式。

I have two tables: DeptMaster and LaborMaster where one DeptMaster has many LaborMasters. These are legacy tables from an old database so they don't follow Code First naming conventions. 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; }
}

在我OnModelCreating,我试图使用的hasMany,但目前还不清楚我怎么会让DDeptNum母公司的实体框架知道如何LCompanyNum,LDivisionNum和LDeptNum孩子回指DCompanyNum,DDivisionNum,而当领域不要被名称相匹配。同样,我将preFER使用属性,但我不知道这是否是可能的。我愿意接受任何。谢谢你。

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. Again, I would prefer using attributes but I'm not sure if it's possible. I'm open to either. Thank You.

推荐答案

您只需要添加 [ForeignKey的] 属性前三属性:

You just need to add the [ForeignKey] attribute for the first three properties:

[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; }
}

或者可以选择把 [ForeignKey的] 属性上的导航属性:

Or alternatively put the [ForeignKey] attribute on the navigation property:

    [ForeignKey("LCompanyNum, LDivisionNum, LDeptNum")]
    public virtual DeptMaster DeptMaster{ get; set; }

下面的属性名的顺序很重要,它必须遵循列的顺序。

Here the order of the property names matters, it must follow the column order.

使用流利的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 });