ADO.net实体框架:一个独立的实体更新只有certian性能 [英] ADO.net Entity Framework: Update only certian properties on a detached entity

查看:170
本文介绍了ADO.net实体框架:一个独立的实体更新只有certian性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于ADO.net实体框架:一个独立的实体更新只有certian性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆