EF 4自我追踪实体无法按预期运作 [英] EF 4 Self Tracking Entities does not work as expected

查看:136
本文介绍了EF 4自我追踪实体无法按预期运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF4自我跟踪实体(VS2010 Beta 2 CTP 2和新的T4生成器).但是,当我尝试更新实体信息时,它没有按预期更新到数据库.

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.

我设置了2个服务呼叫.一个用于GetResource(int id),它返回一个资源对象.第二个调用是SaveResource(Resource res);这是代码.

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.

在我看来,ChangeTracker.State始终显示为未更改".

It seems to me that ChangeTracker.State is always show as "Unchanged".

这段代码有什么问题吗?

anything wrong in this code?

推荐答案

这可能是一个远景...但是:

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.

自跟踪实体(STE)直到将其连接到ObjectContext时才记录更改,这种想法是,如果将它们连接到ObjectContext,它可以为它们记录更改,并且毫无意义地进行两次相同的工作

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.

STE在使用WCF在客户端反序列化后即开始在没有ObjectContext的情况下实例化为一个层后,便开始跟踪.

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.

希望这会有所帮助

亚历克斯

这篇关于EF 4自我追踪实体无法按预期运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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