简体   繁体   English

使用Entity Framework 4.1,如何使用不同的字段名称配置与复合键的一对多关系?

[英]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.

相关问题 如何使用实体框架流利的API配置多对多关系 - How to configure many to many relationship using entity framework fluent API 如何使用 Entity Framework Core 建立一对多关系? - How do I set up a one-to-many relationship using Entity Framework Core? 如何配置Entity Framework级联删除以正确处理一对多关系? - How do I configure Entity Framework cascade delete to work properly with a one to many relationship? 如何使用Entity Framework Core Fluent API配置一对多关系 - How to configure one to many relationship using Entity Framework Core fluent API 一对多关系,配置在实体框架中引用哪个字段 - One to many relationship, configure which field is referenced in Entity Framework 如何与实体框架建立一对多关系? - How do I represent a one to many relationship with the Entity Framework? 在Entity Framework中以多对多关系映射复合外键 - Mapping composite foreign keys in a many-many relationship in Entity Framework 如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系 - How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database 使用实体框架,如何反映多对多关系并为正在创建的新实体添加存在的实体? - Using Entity Framework, how do I reflect a many to many relationship and add entites that exist to a new entity being created? 使用 MVC 3 Entity Framework Code-First,如何以一对多的关系创建一个新的“多”object? - Using MVC 3 Entity Framework Code-First, how do you create a new “many” object in a one-to-many relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM