Tuesday, 15 January 2013

c# - How can I map 2 non-hierarchical classes to the same table with a defining column & value in Entity Framework? -



c# - How can I map 2 non-hierarchical classes to the same table with a defining column & value in Entity Framework? -

so there way map 2 hierarchical classes in ef same table defining column , value (http://msdn.microsoft.com/en-us/data/jj591617.aspx#2.4). wondering if there's way map 2 non-hierarchical classes same table in same manner.

example:

class user guid id string name guid? groupid class grouping guid id string name table uid id pk varchar name bit isgroup uid groupid nullable

i can't alter schema of table, solution i've come far create view user , view group.

mo,

i think @ end of day after user grouping contains users belong it. if that's case here started. remember not meant "best practice" illustration , running. need tweak code exact situation.

here's basic jest of it. need give grouping property of type list...then populate list of users database query. in below illustration wrote method handles populating list. in practice don't have populate list here instantiate usergroup , populate list of users within action method. said in not best practice quick example.

here example.

public class usergroup { class user { private int id { get; set; } private string name { get; set; } private guid grpid { get; set; } public int userid { { homecoming id; } set { id = value; } } public string username { { homecoming name; } set { name = value; } } public guid groupid { { homecoming grpid; } set { grpid = value; } } public user() { } } class grouping { private guid id { get; set; } private string name { get; set; } public list<user> usersingroup = new list<user>(); public guid groupid { { homecoming id; } set { id = value; } } public string groupname { { homecoming name; } set { name = value; } } public group() { } } public usergroup() { } public grouping getusergroup() { grouping x = new group(); guid newgroupid = guid.newguid(); x.groupid = newgroupid; var userquery = mydb.where(n => n.myfield == myconditon).select(n => new { n.userid,//i'm assuming database query returns field userid n.username//i'm assuming database query returns field username }); foreach(var user in userquery) { usergroup.user y = new usergroup.user(); y.groupid = newgroupid; y.userid = user.userid; y.username = user.username; x.usersingroup.add(user); } homecoming x; } }

i hope understood question , illustration points in right direction. best wishes, bill

c# entity-framework fluent

No comments:

Post a Comment