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

ADO.net实体框架:仅更新分离实体上的certian属性,.net,vb.net,entity-framework,ado.net,.net,Vb.net,Entity Framework,Ado.net,我想在不首先从数据库加载实体的情况下更新实体 我已经做到了这一点,但只是通过了解所有的实体属性,然后使用“attachto”方法 我的问题是我不希望我的应用程序需要记住所有属性。例如: Dim customerEntitiy As New shopper customerEntitiy.shopper_id = CustomerData.CustomerID customerEntitiy.market_code = CustomerData.MarketSector customerE

我想在不首先从数据库加载实体的情况下更新实体

我已经做到了这一点,但只是通过了解所有的实体属性,然后使用“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

我认为不可能在不首先从数据库加载的情况下使用保存更改来更新实体。您拥有的代码将生成insert语句,而不是update语句


您可以使用只更新特定文件的存储过程来完成所要完成的任务。

我发现,基本上您可以使用存根,附加它,然后只设置要更新的道具。实体框架将只更新更改的内容

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()
这将绕过“创建”日期字段

看看这篇帖子:

我可以改为通过Linq to Sql执行此操作吗?我没有使用Linq to Sql,但我认为您会遇到同样的问题。虽然可以执行您想要的操作,但不推荐这样做,因为实体可能不会直接链接到表。例如,我使用与视图关联的实体,并使用过程进行更新。这种方法会产生非常不希望出现的效果……您能告诉我们您在本文中所说的“存根”是什么意思吗。我们也遇到了同样的问题,希望知道您是如何解决的。基本上,您是在创建一个新的db对象,然后为其分配主键(.shopper_id=PK),然后使用附件将其放到上下文中。换一下你的东西,然后点击save。