ADO.net实体框架:只更新分离实体上的certian属性

问题描述:

我想更新一个实体而不先从数据库加载实体。ADO.net实体框架:只更新分离实体上的certian属性

我已经完成了这一点,但只知道所有的实体属性,然后使用“attachto”方法。

我的问题是我不希望我的应用程序需要记住所有的属性。例如:

Dim customerEntitiy As New shopper 
customerEntitiy.shopper_id = CustomerData.CustomerID 
customerEntitiy.market_code = CustomerData.MarketSector 
customerEntitiy.email = CustomerData.Email 
customerEntitiy.modified = DateTime.Now 
context.AttachTo("shopper", customerEntitiy) 
context.SaveChanges() 

该实体还有一个“创建”字段。我不希望将这个“创建”日期一直通过我的n层应用程序。如何在保存到数据库时“不更新”该字段?谢谢! Paul

+0

虽然可以做你想做的,它不推荐,因为实体可能不会dierectly挂表。例如,我使用与视图相关的实体,并使用过程进行更新。这种方法会有非常不希望的影响... – 2009-07-23 17:28:31

我想通了,基本上你使用了一个存根,附加它,然后只设置你想要更新的道具。实体框架只会更新已更改的内容。

Dim customerEntitiy As New commerce_shopper 
customerEntitiy.shopper_id = CustomerData.CustomerID 'this is the primary key' 
context.AttachTo("commerce_shopper", customerEntitiy) 
customerEntitiy.market_code = CustomerData.MarketSector 
customerEntitiy.email = CustomerData.Email 
customerEntitiy.modified = DateTime.Now 
context.SaveChanges() 

这将绕过“创建”日期字段。

我不认为有可能使用保存更改来更新实体,而无需先从数据库中加载它。您拥有的代码将生成一个插入语句,而不是更新。

您可以使用存储过程完成您正在尝试执行的操作,该过程只更新特定的文件。

+0

我可以通过Linq到Sql来做到这一点吗? – 2009-07-23 12:52:20

+0

我没有使用Linq到SQL,但我认为你会遇到同样的问题。 – 2009-07-23 13:21:50