简体   繁体   中英

Entity Framework Foreign Key customization

I am using Entity Framework 6.0, I know the purpose of FK, what I need is a little bit tweaking on it.

This is example code snippet

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

Here by default the join will be

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

Here by default ef queries

[table1_fk] = [table2_pk]

What I want is

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

If it isn't possible, can you please provide a best alternative to achieve this?

Thanks,

It depend on what strategy you want to use. For Code First it could be done like this. Foreign key EF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM