c# - EF Code First : Linking on non-key property in configuration -


i'm looking perform seemingly difficult ef code first relationship between of entities. i've searched around stack overflow web can't seem come anything. have simple class outlined below. pk program id. has (non-pk) familyid value. there no "families" table or similar. moreover, "familyid" not fk id value of different program entity. in mind, consider "grouping" of sorts.

public class program {     public int id { get; set; }      public virtual list<program> relatedprograms { get; set; }     public int? familyid { get; set; } } 

let me explain further sample data.

id : 1, familyid : 17
id : 2, familyid : 17
id : 3, familyid : 18
id : 4, familyid : 17

using ef code first in configuration i'd add logic populate "relatedprograms" property program 1, 2, , 4 if query of 3 (e.g. point here related each other through arbitray property). if query program 3, there'd 1 record in relatedprograms property , program 3 since nothing else shares same familyid.

i think logic needs similar following except following automatically makes references id column behind scenes; need linking solely based upon familyid column of program being accessed , familyid value of other programs in database.

this.hasmany(p => p.relatedprograms).withrequired().hasforeignkey(p => p.familyid);

any provide on appreciated.

thanks!

if getting right, want know group of programs based on attribute, family id? sounds me not property of program object, group of programs. have program id , family id (which sounds more category me) in program entity, , when need know related programs, query entity:

var relatedprograms = context.programs.where(p => p.familyid == myfamilyid).tolist(); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -