实体框架外键自定义

本文关键字:自定义 框架 实体 | 更新日期: 2023-09-27 17:57:03

我正在使用实体框架 6.0,我知道 FK 的目的,我需要对它进行一些调整。

这是示例代码片段

class CommonMaster
{
      public int ID {get;set;}
      public string Name {get;set;}
      public string Value {get;set;}
      public string Type {get;set;}
}
class BankDetails
{
  //all relevant fields
  public int CustomerType {get;set;}  //expected by ef
  public string CustomerType {get;set;} //the one i want
  [ForeignKey("CustomerType")] //expected by ef
  public virtual CommonMaster CommonMaster_CustomerType {get;set;}
}

默认情况下,此处的连接将是

from BankDetails bd join CommonMaster cm on bd.CustomerType = cm.Value

此处默认 ef 查询

[table1_fk] = [table2_pk]

我想要的是

[table1_somefield with codevalue]= [table2 _ code value]

如果不可能,您能否提供实现这一目标的最佳替代方案?

谢谢

实体框架外键自定义

这取决于您要使用的策略。对于代码优先,可以这样完成。外键 EF