Intercepting ADD to detect if it is an ADD through a collection in Entity Framework 5 -
i have kinda 2 questions can answered separately.
q#1
i trying save round trips database server.
here's algo:
- insert 2 entities (to ids generated database)
- use ids returned call stored procedure passing ids
the stored procedure takes ids , populates adjacency list table using store directed acyclic graph.
currently have round-trip rdbms each parent-child relationship, plus 1 insert of entities.
i known stuff this:
public override int savechanges() { foreach (var entry in this.changetracker.entries().where(e => e.state == system.data.entitystate.added).tolist()) { if (entry.entity irobot) { entry.reference("owner").currentvalue = skynet; } } return base.savechanges(); } so wondering if there way can detect entitystate.added "add" done similar following code:
var robot = new robot(); skynet.robots.add(robot); db.add(skynet); db.savechanges(); so can this: (note psuedocode)
public override int savechanges() { foreach (var entry in this.changetracker.entries().where(e => e.state == entitystate.**addedtocollection**).tolist()) { db.relate(parent: skynet, child: entry.entity); } return base.savechanges(); } q#2
is there anyway call stored procedure part of same "trip" database after calling savechanges()?
question 1
you can detect state of entity by
db.entry(robot).state after line
skynet.robots.add(robot); the entitystate of robot added. however, in pseudocode not clear skynet variable comes from. if add skynet in code snippet do:
foreach( var skynet in changetracker.entries() .where(e => e.state == entitystate.added) .select (e => e.entity) .oftype<skynet>()) { foreach(var robot in skynet.robots .where(r => db.entry(r).state == entitystate.added)) { db.relate(parent: skynet, child: robot); } } question 2
you can't call stored procedure in 1 roundtrip, require nhibernate's multi query. but, can wrap savechanges , stored procedure call in 1 transaction (which think mean) using transactionscope:
using (transactionscope scope = new transactionscope()) { // stored procedure call here. db.savechanges(); scope.complete(); }
Comments
Post a Comment