According to microsoft docs on disconnected entities, if the entity is not using auto-generated keys, then the application must decide whether the entity should be inserted or updated:
public static void InsertOrUpdate(BloggingContext context, Blog blog)
{
var existingBlog = context.Blogs.Find(blog.BlogId);
if (existingBlog == null)
{
context.Add(blog);
}
else
{
context.Entry(existingBlog).CurrentValues.SetValues(blog);
}
context.SaveChanges();
}
Suppose I use this in the else block (instead of the one shown above):
else
{
context.Update(blog);
}
Is it advisable? How is .Update
different from .Entry(existingBlog).CurrentValues.SetValues(blog)
especially in terms of the complexity? Which will be less costly to call? I can't seem to find information on how the 2 methods are implemented and their complexity.