c# - Entity Framework intermediate table - insert new record using existing unrelated ids? -



c# - Entity Framework intermediate table - insert new record using existing unrelated ids? -

so have 2 tables, , b. each table set so:

a: itemaid, itemaproperty1, itemaproperty2, etc. b: itembid, itembproperty1, itembproperty2, etc.

i have record in table , record in table b. these records in no way related each other.

i have 3rd table, c set so:

c: itemaid, itembid

i want create new record in table c existing records in tables , b. c not available using intellisense. everywhere i've looked, suggestion build object exists, , object(s) b exist, add together b (or vice versa), add... , because objects exist in database, ef update , link objects in intermediate table. (from insert/update many many entity framework . how do it?) i.e.:

/**** rough psuedocode ****/ var = context.first(a => a.id == passedinaid); var b = context.first(b => b.id == passedinbid); a.bproperty.add(b); context.add(a); context.savechanges()

however, in case, objects have no properties relates them not working.

i know can write stored proc this, possible ef?

so, discovered digging around more...

even though itema , itemb have no direct relations each other, appears fact there linking table contains primary ids results in icollection of itema within itemb object, , vice versa. work, needed was, in fact, 2 objects wanted link, add together 1 other's collection of said objects, , add. actual (imitation) code of how works is:

using ( var ctx = new model()) { var item1 = ctx.itema.firstordefault(i => i.id == itemaid); var item2 = ctx.itemb.firstordefault(j => j.id == itembid); item1.itembs.add(item2); ctx.savechanges(); }

of course, first() work if existence of objects verified, , "item1.itembs.add()" has "itembs" because of ef's annoying tendency pluralize everything.

c# .net entity-framework

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -