今天发现几个同事都不知道在EF中更新detached对象的正确(or 官方)方法:
private
static
void
ApplyItemUpdates(SalesOrderDetail updatedItem)
{
// Define an ObjectStateEntry and EntityKey for the current object.
EntityKey key;
object originalItem;
using
(AdventureWorksEntities advWorksContext =
new
AdventureWorksEntities())
{
try
{
// Create the detached object's entity key.
key = advWorksContext.CreateEntityKey("SalesOrderDetail"
, updatedItem);
// Get the original item based on the entity key from the context
// or from the database.
if
(advWorksContext.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyPropertyChanges method to apply changes
// from the updated item to the original version.
advWorksContext.ApplyPropertyChanges
(
key.EntitySetName, updatedItem);
}
advWorksContext.SaveChanges();
}
catch
(InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
http://msdn.microsoft.com/en-us/library/bb896248.aspx
关于detached对象以及Attach的解释:
http://msdn.microsoft.com/en-us/library/bb896271.aspx
分享到:
相关推荐
Z.EntityFramework.Extensions 破解 注册机,详细破解方法
Z.EntityFramework.Extensions注册机 破解 内含使用截图
Entity Framework 6 (EF6) is a tried and tested object-relational mapper (O/RM) for .NET with many years of feature development and stabilization.
Entity Framework Core Cookbook - Second Edition by Ricardo Peres English | 9 Nov. 2016 | ISBN: 1785883305 | 340 Pages | MOBI/EPUB/PDF+Code Files | 6.2 MB Entity Framework is a highly recommended ...
PM> Install-Package Z.EntityFramework.Plus.EFCore Z.EntityFramework.Plus.EF6 PM> Install-Package Z.EntityFramework.Plus.EF6 Z.EntityFramework.Plus.EF5 PM> Install-Package Z.EntityFramework.Plus....
Entity Framework 4.3
Z.EntityFramework.Extensions注册机 , 3.12.*任意版本可以用,亲测,绝对好使,不好使退钱
Understand the core concepts of Entity Framework Core, as well process models for existing databases (reverse engineering) and the generation of database schemas from object models (forward ...
Entity Framework 6 Recipes Entity Framework 6 Recipes
EntityFramework Profiler-EFProf SQL跟踪优化建议工具
本书是关于Entity framework code first 的详细介绍,在本书中,你可以学到从无到有的创建基于Entity framework code first的项目
Orleans.Providers.EntityFramework Orleans谷物存储的Entity Framework Core实现。 有一些不错的功能缺失。 我不是特别需要它们,但是如果您有建议或想要帮助,将不胜感激。用法Nuget: ://...
EntityFramework-deneme-2
Entity Framework试水系列-博客园cnblogs.rar
Entity Framework Repository(含依赖注入)
entityframework框架源代码,需要深入了解entityframework的同学可以下载深入学习研究
电子书 Entity Framework 4 In Action
数据库-MSSQL-EntityFramework-SoftUni-问题
ef entityframework服务端 封装dll