实体框架自我跟踪和客户端不跟踪更改
为什么我的客户不跟踪更改。 下面的代码
我在启用了自跟踪实体的服务器 WCF
public class TrialService : ITrialService
{
public Project GetProjectByID(int _projectId)
{
var db = new TrialEntitiesService.FringeNETEntities();
return db.Projects.Include("Items.SubItems").First(s => s.ProjectID == _projectId);
}
public Item UpdateItem(Item _item)
{
var db = new TrialEntitiesService.FringeNETEntities();
_item.Actual = 100000;
db.Items.ApplyChanges(_item);
db.SaveChanges();
return _item;
}
}
和客户端
public MainWindow()
{
InitializeComponent();
using (TrialServiceClient proxy = new TrialServiceClient())
{
radGridView1.ItemsSource = proxy.GetProjectByID(37).Items;
return;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
using (TrialServiceClient proxy = new TrialServiceClient())
{
proxy.UpdateItem((Item)radGridView1.SelectedItem);
}
}
}
假设第一次调用中的项目也会更新。这不是自我跟踪实体所实现的,还是我错过了大图。
如果这不需要自我跟踪实体,那么我将如何最好地实现这一点,而不是从第一个服务器调用手动查找和修改项目。
How come my client isnt tracking changes. My code below
Server WCF with selftracking entities enabled
public class TrialService : ITrialService
{
public Project GetProjectByID(int _projectId)
{
var db = new TrialEntitiesService.FringeNETEntities();
return db.Projects.Include("Items.SubItems").First(s => s.ProjectID == _projectId);
}
public Item UpdateItem(Item _item)
{
var db = new TrialEntitiesService.FringeNETEntities();
_item.Actual = 100000;
db.Items.ApplyChanges(_item);
db.SaveChanges();
return _item;
}
}
and client
public MainWindow()
{
InitializeComponent();
using (TrialServiceClient proxy = new TrialServiceClient())
{
radGridView1.ItemsSource = proxy.GetProjectByID(37).Items;
return;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
using (TrialServiceClient proxy = new TrialServiceClient())
{
proxy.UpdateItem((Item)radGridView1.SelectedItem);
}
}
}
I was assuming that the item in the first call would also be updated. isn't this what Self-Tracking entities achieves or am I missing the big picutre.
If this isn't want Self-Tracking Entites is for then how would i best acheive this rather then manually find and modify the item from the first server call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 STE 时,您不能简单地将服务引用添加到服务中。您必须首先添加对包含 STE 的程序集的引用,并确保在添加服务引用期间重用该程序集中的类型。否则,您的客户将获得不包含自我跟踪功能的实体的新实现。两个演练:
When using STEs you cannot simply Add service reference to the service. You must first add reference to the assembly containing STEs and make sure that types from that assembly are reused during adding service reference. Otherwise your client gets new implementation of entities which don't contain self tracking functionality. Two walkthroughs: