[POCO] [仅限代码,CTP3]附加前的自我跟踪(智能更新)(不同的上下文) [英] [POCO][Code Only,CTP3]Self-Tracking(Smart Update) before attach(Different context)

查看:49
本文介绍了[POCO] [仅限代码,CTP3]附加前的自我跟踪(智能更新)(不同的上下文)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

公共课Person {
public long Id {get; set;}
public string Name {get; set;}
public string Addr {get; set;}
}

var person = new Person();

using(context1 = new SampleContext()){
person = context1.Persons.Single();
}

person.Name =" test" ;;


使用(context2 = new SampleContext()){

context2.Persons.Attach(person);
context2.ObjectStateManager.ChangeObjectState(person,EntityState.Modified);
context2.SaveChanges();

}



解决方案

< blockquote>

使用STEs你应该使用context.Persons.ApplyChanges(person),而不是附加& ChangeObjectState。这将应用从参数可到达的整个对象图中的更改。


默认情况下,STE生成器不会生成用于跟踪单个属性更改的代码。要覆盖此行为,请在Types.tt文件的开头附近找到以下行:


    OriginalValueMembers originalValueMembers = new OriginalValueMembers(allMetadataLoaded,metadataWorkspace,ef);


并将'allMetadataLoaded'参数替换为常量'false'。


   public class Person{
     public long Id{get ;set ;}
     public string Name{get;set;}
     public string Addr{get ;set;}
   }

    var person=new Person();

using (context1 = new SampleContext()){
person= context1.Persons.Single();
}

person.Name="test";


using(context2 = new SampleContext()){

context2.Persons.Attach(person);
context2.ObjectStateManager.ChangeObjectState(person, EntityState.Modified);
context2.SaveChanges();

}


解决方案

With STEs you should use context.Persons.ApplyChanges(person), not attach & ChangeObjectState. This applies changes from the entire object graph reachable from the argument.

By default the STE generator does not generate code for tracking individual property changes. To override this behavior find the following line near the beginning of your Types.tt file:

    OriginalValueMembers originalValueMembers = new OriginalValueMembers(allMetadataLoaded, metadataWorkspace, ef);

and replace the 'allMetadataLoaded' argument to constant 'false'.


这篇关于[POCO] [仅限代码,CTP3]附加前的自我跟踪(智能更新)(不同的上下文)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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