EF 4 自跟踪实体未按预期工作
我正在使用 EF4 自跟踪实体(VS2010 Beta 2 CTP 2 加上新的 T4 生成器)。但是,当我尝试更新实体信息时,它不会按预期更新到数据库。
我设置了 2 个服务呼叫。一个用于 GetResource(int id),它返回一个资源对象。第二个调用是 SaveResource(Resource res);这是代码。
public Resource GetResource(int id)
{
using (var dc = new MyEntities())
{
return dc.Resources.Where(d => d.ResourceId == id).SingleOrDefault();
}
}
public void SaveResource(Resource res)
{
using (var dc = new MyEntities())
{
dc.Resources.ApplyChanges(res);
dc.SaveChanges();
// Nothing save to database.
}
}
//Windows Console Client Calls
var res = service.GetResource(1);
res.Description = "New Change"; // Not updating...
service.SaveResource(res);
// does not change anything.
在我看来,ChangeTracker.State 始终显示为“未更改”。
这段代码有什么问题吗?
I am using EF4 Self Tracking Entities (VS2010 Beta 2 CTP 2 plus new T4 generator). But when I try to update entity information it does not update to database as expected.
I setup 2 service calls. one for GetResource(int id) which return a resource object. the second call is SaveResource(Resource res); here is the code.
public Resource GetResource(int id)
{
using (var dc = new MyEntities())
{
return dc.Resources.Where(d => d.ResourceId == id).SingleOrDefault();
}
}
public void SaveResource(Resource res)
{
using (var dc = new MyEntities())
{
dc.Resources.ApplyChanges(res);
dc.SaveChanges();
// Nothing save to database.
}
}
//Windows Console Client Calls
var res = service.GetResource(1);
res.Description = "New Change"; // Not updating...
service.SaveResource(res);
// does not change anything.
It seems to me that ChangeTracker.State is always show as "Unchanged".
anything wrong in this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题并找到了解决方案。
看来,要使自跟踪实体自动开始跟踪,您需要在添加服务引用之前引用您的 STE 项目。
这样,Visual Studio 会生成一些 .datasource 文件,从而完成最后的任务。
我在这里找到了解决方案:
http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx
至于手动启动跟踪,看来您在客户端没有这些方法。
希望它有帮助...
I had the same exact problem and found the solution.
It appears that for the self-tracking entities to automatically start tracking, you need to reference your STE project before adding the service reference.
This way Visual Studio generates some .datasource files which does the final trick.
I found the solution here:
http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx
As for starting the tracking manually, it seems that you do not have these methods on the client-side.
Hope it helps...
这可能是一个遥远的机会......但是:
我假设您的服务实际上位于另一个层?如果您在同一层进行测试,则会遇到问题。
自跟踪实体 (STE) 在连接到 ObjectContext 之前不会记录更改,其想法是,如果它们连接到 ObjectContext,它可以为它们记录更改,并且没有必要重复执行相同的工作。
一旦使用 WCF 在客户端上反序列化 STE,即一旦将它们具体化到没有 ObjectContext 的层,STE 就会开始跟踪。
如果您查看生成的代码,您应该也能够了解如何手动打开跟踪。
希望这对
亚历克斯有帮助
This is probably a long shot... but:
I assume your Service is actually in another Tier? If you are testing in the same tier you will have problems.
Self Tracking Entities (STEs) don't record changes until when they are connected to an ObjectContext, the idea is that if they are connected to a ObjectContext it can record changes for them and there is no point doing the same work twice.
STEs start tracking once they are deserialized on the client using WCF, i.e. once they are materialized to a tier without an ObjectContext.
If you look through the generated code you should be able to see how to turn tracking on manually too.
Hope this helps
Alex
您必须在客户端和服务之间与 STE 共享程序集 - 这是要点。然后,在添加服务引用时,请确保选中“重用引用程序集中的类型”。
原因是 STE 包含无法通过“添加服务引用”传输的逻辑,因此您必须共享这些类型才能在客户端上也具有跟踪逻辑。
You have to share assembly with STEs between client and service - that is the main point. Then when adding service reference make sure that "Reuse types in referenced assemblies" is checked.
The reason for this is that STEs contain logic which cannot be transfered by "Add service reference", so you have to share these types to have tracing logic on client as well.
阅读 Daniel Simmons 的以下提示后,STE 开始跟踪。这是全文的链接。 http://msdn.microsoft.com/en-us/magazine/ee335715。 ASPX
因此,在客户端中,请确保不使用添加服务引用来获取代理,而是通过以下代码访问服务。
After reading the following tip from Daniel Simmons, the STE starts tracking. Here is the link for the full article. http://msdn.microsoft.com/en-us/magazine/ee335715.aspx
so in the client make sure you don't use add service reference to get the proxy instead access service through following code.
如果您使用没有 WCF 的 STE,您可能必须手动调用 StartTracking()。
If you are using STEs without WCF you may have to call StartTracking() manually.