【问题标题】:Entity Framework Foreign Key customization实体框架外键自定义
【发布时间】:2015-06-18 04:24:50
【问题描述】:

我正在使用 Entity Framework 6.0,我知道 FK 的用途,我需要对其进行一些调整。

这是示例代码sn-p

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]

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

谢谢,

【问题讨论】:

标签: c# sql .net sql-server


【解决方案1】:

这取决于您要使用的策略。对于 Code First,可以这样做。 Foreign key EF

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多