EntityFramework Migrations provides an extension method on DbSet<T>
, specifically for seeding data during a migration:
void AddOrUpdate<TEntity>(this IDbSet<TEntity> set, params TEntity[] entities);
Is this safe to use in "regular" code, i.e. not for seeding data during a migration ?
var blog = ...//detached instance from a request
using (var context = new BloggingContext())
{
context.Blogs.AddOrUpdate(blog);
context.SaveChanges();
}
It seems to work fine, but I'm wondering if it has any downsides compared to the "traditional" 'detached entity' sceario - as described, for instance, on MSDN (last part of the article):
using (var context = new BloggingContext())
{
context.Entry(blog).State = blog.BlogId == 0 ?
EntityState.Added :
EntityState.Modified;
context.SaveChanges();
}