views:

934

answers:

3

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?

+3  A: 

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

Alex James
Service is in the same tier, but different layer. my idea is write everything in the same tier (different layers) but later change to different tiers if needed. In the above example ObjectContext is going out of scope for each service call, so why STE not tracking. or STE only work with WCF scenario?. what is the best approach (POCO, STE or something) with EF 4 in the above scenario in both performance wise and extensibility?
ashraf
Tracking is turned on by deserialization, so it you are in the same tier, it isn't being turned on, despite your ObjectContext going out of scope.
Alex James
A: 

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

Make certain to reuse the Self-Tracking Entity template’s generated entity code on your client. If you use proxy code generated by Add Service Reference in Visual Studio or some other tool, things look right for the most part, but you will discover that the entities don’t actually keep track of their changes on the client.

so in the client make sure you don't use add service reference to get the proxy instead access service through following code.

var svc = new ChannelFactory<IMyService>("BasicHttpBinding_IMyService").CreateChannel();
var res = svc.GetResource(1);
ashraf
A: 

If you are using STEs without WCF you may have to call StartTracking() manually.

Sep15ms