c# - Entity Framework and adding POCOs without adding child objects -
so perhaps i'm addressing problem wrong way, wanted sentiment fine people on stackoverflow how more correctly this.
i've got programme has retrieve info repository around entity framework 6.0 code-first context, work on info contained , adds new record database.
anyway, here's simplified @ class i'm retrieving ef through repository:
public class product { public int id { get;set; } public virtual productcategory category { get;set; } public string name { get;set; } }
i build processedproduct next definition , pass in retrieved product baseproduct:
public class processedproduct { public int id { get;set; } public virtual product baseproduct { get;set; } }
i utilize repository layer saw on ef lesson on pluralsight , have purposed here. i've added relevant bits below:
public class mycontext : basecontext<mycontext>, imycontext { //lots of idbsets each context public void setmodified(object entity) { entry(entity).state = entitystate.modified; } public void setadd(object entity) { entry(entity).state = entitystate.added; } } public class myrepository : imyrepository { private readonly imycontext _context; public myrepository(iunitofwork uow) { _context = uow.context imycontext; } public processedproduct findprocessedproduct(int id) { homecoming _context.processedproducts.find(id); } public productcategory findcategory(int id) { homecoming _context.categories.find(id); } public int addprocessedproductwithoutproduct(processedproduct newrecord) { newrecord.product = null; save(); homecoming newrecord.id; } public int updateprocessedproductwithproductbutwithoutchildproperties(int processedproductid, int productid) { var processedproduct = findprocessedproduct(processedproductid); processedproduct.baseproduct = findproduct(productid); processedproduct.baseproduct.category = null; _context.setmodified(product); save(); homecoming processedproduct.id; } public int updateproductchildren(int processedproductid, int categoryid) { var processedproduct = findprocessedproduct(processedproductid); var category = findcategory(categoryid); processedproduct.baseproduct.category = category; _context.setmodified(product); save(); homecoming processedproduct.id; } }
and finally, here's portion pulls together:
try { //create processed product without product instance var processedproductid = repo.addprocessedproductwithoutproduct(finishedproduct); //now, update processed product record product. way, don't create //duplicate product. processedproductid = repo.updateprocessedproductwithproductbutwithoutchildproperties(processedproductid, product.id); //finally, update category processedproductid = repo.updateproductchildren(processedproductid, product.category.id); //done! }
when effort insert processedproduct ef, correctly creates processedproduct record, creates new product , new category row. i've tried manually changing alter tracking each object processedproduct 'added' , others either 'modified' or 'unchanged', foreign key reference exceptions thrown entity framework.
my "fix" break number of different calls:
i create new processedproduct record, assign product value null. i query processedproduct record id, query appropriate product id , assign product newly retrieved processedproduct record. however, have null out category property or else add together new duplicate category record. save , processedproduct record modified. finally, query processedproduct 1 time more productcategory , assign productcategory category property of processedproduct.baseproduct. can save 1 time more , i've created records need without making of duplicates.however, approach seems quite convoluted since wanted save new parent record , not create duplicate kid records. there improve way go doing i'm missing? thanks!
edit: , guess larger question have complex object whole bunch of these kid complex objects. what's easiest way create new parent without having go through entire graph of kid objects update parent them 1 layer @ time?
i highly recommend not setting product & category navigation properties when editing. saw when add together graph of processed product (with product & category attached) ef context, it's marking in graph added , inserts on everything. pattern recommend (and nikolai suggested in comment, up-vote comment did :)) include fk ids in entity , set values, not navigations. e.g. newrecord.productid=theproductidvalue.
i've had many people cry "but foreign keys? ewwww! create classes dirty , impure!" after see how much easier code things without tangling navigations in these scenarios, have come "okay, worth it!"
btw if talking ef in enterprise course, have whole module dealing problem...it's called bout graphs in disconnected scenarios. :)
c# entity-framework
No comments:
Post a Comment