开发者

RIA Services Repository Save does not work?

Doing my first SL4 MVVM RIA based application and i ran into the following situation: updating a record (EF4,NO-POCOS!!) in the SL-client seems to take place, but values in the dbms are unchanged. Debugging with Fiddler the message on save is (amongst others):

EntityActions.nil� b9http://schemas.microsoft.com/2003/10/Serialization/Arrays^HasMemberChanges�^Id�^ Operation�Update

I assume that this says only: hey! the dbms should do an update on this record, AND nothing more! Is that right?!

I 'm using a generic repository like this:

public class Repository<T> : IRepository<T> where T : class
    {
        IObjectSet<T> _objectSet;
        IObjectContext _objectContext;

        public Repository(IObjectContext objectContext)
        {
            this._objectContext = objectContext;
            _objectSet = objectContext.CreateObjectSet<T>();
        }

        public IQueryable<T> AsQueryable()
        {
            return _objectSet;
        }
        public IEnumerable<T> GetAll()
        {
            return _objectSet.ToList();
        }
        public IEnumerable<T> Find(Expression<Func<T, bool>> where)
        {
            return _objectSet.Where(where);
        }
        public T Single(Expression<Func<T, bool>> where)
        {
            return _objectSet.Single(where);
        }
        public T First(Expression<Func<T, bool>> where)
        {
            return _objectSet.First(where);
        }
        public void Delete(T entity)
        {
            _objectSet.Delete开发者_运维问答Object(entity);
        }
        public void Add(T entity)
        {
            _objectSet.AddObject(entity);
        }
        public void Attach(T entity)
        {
            _objectSet.Attach(entity);     
        }

        public void Save()
        {           
            _objectContext.SaveChanges();
        }
    }

The DomainService Update Method is the following:

[Update]
        public void UpdateCulture(Culture currentCulture)
        {
            if (currentCulture.EntityState == System.Data.EntityState.Detached)
            {
                this.cultureRepository.Attach(currentCulture);
            }
            this.cultureRepository.Save();
        }

I know that the currentCulture-Entity is detached. What confuses me (amongst other things) is this: is the _objectContext still alive? (which means it "will be"??? aware of the changes made to record, so simply calling Attach() and then Save() should be enough!?!?)

What am i missing?

Development Environment: VS2010RC - Entity Framework 4 (no POCOs)

Thanks in advance


You are attaching the culture in the context, but you are not telling the context that the object has actually changed.

The generated code I have on my machine is:

public void UpdateDepartment(Department currentDepartment) {
    if ((currentDepartment.EntityState == EntityState.Detached)) {
        this.ObjectContext.AttachAsModified(currentDepartment, this.ChangeSet.GetOriginal(currentDepartment));
    }
}

What matters is the 'AttachAsModified'.


Timores pointed me in the correct direction, the solution (as far as my problem concerns) is very simple: simply add this method to the repository and we're done:

public void AttachModified(T entity)
{
            _objectSet.Attach(entity);
            _context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);            
} 

Now instead of calling Attach() we call AttachModified().

Thank you Timores!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜