ADO.net实体框架:一个独立的实体更新只有certian性能实体、框架、独立、性能

2023-09-04 00:59:50 作者:偷生.

我想没有首先从数据库加载实体更新实体。

I want to update an entity without loading the entity from the database first.

我已经完成此,而是仅由知道所有的实体属性,然后使用attachto的方法。

I've accomplished this but only by knowing all of the entities properties and then using the "attachto" method.

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

My issues is i don't want my app to need to remember all of the properties. Example:

 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层应用程序,通过这种创造的日期的所有道路。如何我只是保存到数据库中,当不更新的领域?谢谢! 保

That entity also has a "created" field on it. I don't want to pass this "created" date all the way through my n-tier app. How can i just "not update" that field when saving to the database? Thanks! Paul

推荐答案

我想通了,基本上你使用存根代替,将其安装,然后只设置要更新的道具。实体框架将只更新改变了的东西。

I figured it out, basically you use a stub instead, attach it, then only set the props you want to be updated. The entity framework will only update the things changed.

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()

这绕过了创建日期字段。

This bypasses the "created" date field.