惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
爱范儿
爱范儿
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
博客园_首页
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
V
Visual Studio Blog
Jina AI
Jina AI
博客园 - Franky
量子位
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence

博客园 - Terry Sun

Ubuntu安装Java7 SDK Postgres整理 RESTful架构API的设计误区 解决createdb: could not connect to database postgres: FATAL: Peer authentication failed for user &quot;postgres&quot; 如何升级Nginx到最新稳定版 The remote name could not be resolved:&#39;maps.googleapis.com&#39; 的解决办法 关于Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005的解决办法 [iOS]黑色状态栏 圆角内容区 No connection could be made because the target machine actively refused it 127.0.0.1:808 的解决办法 Google Map V3--geocode与fitBounds方法的同步操作 安装SSL证书-解决导入证书时的ASN1 bad tag value met错误 解决 The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) - Terry Sun &lt;转&gt;用HTML判断IE版本 关于Master Page的css和js文件引用问题 ASP.NET Mvc 2.0 - 2. 异步Controller执行流程时序图 [转载]ASP.NET 2.0 中的异步页 ASP.NET Mvc 2.0 - 1. Areas的创建与执行 深入分析 ASP.NET Mvc 1.0 – 4. 使用ModelBinder绑定Action的参数 深入分析 ASP.NET Mvc 1.0 – 3. Controller.Execute(Request)-ActionInvoker.InvokeAction()
ADO.NET Entity Framework--不再查询直接更新数据
Terry Sun · 2011-07-21 · via 博客园 - Terry Sun
    public static class ContextObject_Helper
{
public static int UpdateEntity<T>(this ObjectContext objectContext, T entity, string entitySetName) where T: EntityObject
{
// Attach specify entity
objectContext.AttachTo(entitySetName, entity);// Get object state by specify entity.
System.Data.Objects.ObjectStateEntry state = objectContext.ObjectStateManager.GetObjectStateEntry(entity);// Get all members from entity.
MemberInfo[] members = entity.GetType().GetMembers();// Get all properties except EntityKey.
IEnumerable<MemberInfo> properties = members.Where(m => m.MemberType == MemberTypes.Property && m.Name != "EntityKey");foreach (MemberInfo mInfo in properties)
{
//Find property that used System.Runtime.Serialization.DataMemberAttribute.
object[] attrs = mInfo.GetCustomAttributes(typeof(System.Runtime.Serialization.DataMemberAttribute), true);if (attrs.Length > 0)
{
object[] emdAttrs = mInfo.GetCustomAttributes(typeof(System.Data.Objects.DataClasses.EdmScalarPropertyAttribute), true);
if (emdAttrs.Length > 0)
{
var edm
= emdAttrs[0] as System.Data.Objects.DataClasses.EdmScalarPropertyAttribute;
if (!edm.EntityKeyProperty)
state.SetModifiedProperty(mInfo.Name);
}
}
}
return 0;
}
}

ADO.NET Entity提共的默认更新数据的方法是:

  1. 先找出要更新的对象(访问一次数据库)
  2. 赋新值
  3. 调用 xxxEntities.SaveChange() 方法(需要再次访问一次数据库)

一个update操作需要访问2次数据库, 多用户大数据量的环境下这样的性能确实不怎么样, 理想化的操作当然是只进行第2,3步.

上面的代码是一个扩展方法,任何ObjectContext的子类也就是xxxEntities才可以调用UpdateEntity方法

  1. 第一个参数entity就是要更新的实体对象
  2. 第二个参数entitySetName就是这个实体对象的 Entity Set Name, 通过entity是不能获得到的, 只有在调用UpdateEntity方法时传进来

以pubs数据库的author表为例, au_id是主键, 下面的代码将会更新author表中au_id为427-17-2319这条数据

PubsEntities pubsEnt = new PubsEntities();
Author auth
= new Author()
{
au_id
= "427-17-2319",
au_lname
= "Dull",
au_fname
= "Ann",
phone
= "415 836-7128",
address
= "3410 Blonde St.",
city
= "Palo Alto",
state
= "CA",
zip
= "99999",
contract
= true
};

pubsEnt.UpdateEntity(auth, pubsEnt.Authors.EntitySet.Name);
pubsEnt.SaveChanges();

UpdateEntity方法不足之处是需要传递 pubsEnt.Authors.EntitySet.Name这个参数, 有什么好办法在UpdateEntity内部可以获得这个属值?