entity framework - EF: Is it OK to use DbSet<T>.AddOrUpdate() outside of migrations? -
entityframework migrations provides extension method on dbset<t>, seeding data during migration:
void addorupdate<tentity>(this idbset<tentity> set, params tentity[] entities); is safe use in "regular" code, i.e. not seeding data during migration ?
var blog = ...//detached instance request using (var context = new bloggingcontext()) { context.blogs.addorupdate(blog); context.savechanges(); } it seems work fine, i'm wondering if has downsides compared "traditional" 'detached entity' sceario - described, instance, on msdn (last part of article):
using (var context = new bloggingcontext()) { context.entry(blog).state = blog.blogid == 0 ? entitystate.added : entitystate.modified; context.savechanges(); }
well, according julie lerman authority in ef, should use addorupdate method in migrations, check blog post:
"it meant use seeding data during migrations.it looks nice method add apps that’s not it’s purpose."
...
first, execute query in database looking record whatever supplied key (first parameter) matches mapped column value (or values) supplied in
addorupdate. little loosey-goosey matching fine seeding design time data.
as can see, has additional cost because, before add or update, executes query searching if record exist. so, best way use code mention @ end of post.
Comments
Post a Comment