RIA 服务存储库保存不起作用?

发布于 2024-08-26 01:47:49 字数 2282 浏览 3 评论 0原文

在做我的第一个基于 SL4 MVVM RIA 的应用程序时,我遇到了以下情况: SL 客户端中的记录(EF4,NO-POCOS!!)似乎已更新,但 dbms 中的值未更改。使用 Fiddler 调试保存时的消息是(除其他外):

EntityActions.nil� b9http://schemas.microsoft.com/2003/10/Serialization/Arrays^HasMemberChanges�^Id�^ 操作�更新

我认为这仅说明: 嘿!数据库管理系统应该对此记录进行更新,仅此而已!是这样吗?

我正在使用这样的通用存储库:

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.DeleteObject(entity);
        }
        public void Add(T entity)
        {
            _objectSet.AddObject(entity);
        }
        public void Attach(T entity)
        {
            _objectSet.Attach(entity);     
        }

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

DomainService 更新方法如下:

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

我知道 currentCulture-Entity 已分离。让我困惑的(除其他外)是: _objectContext 还活着吗? (这意味着它“将”???知道对记录所做的更改,因此只需调用 Attach() 然后 Save() 就足够了!?!?)

我错过了什么?

开发环境:VS2010RC - Entity Framework 4(无 POCO)

提前致谢

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.DeleteObject(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝在阴天搁浅。 2024-09-02 01:47:49

您将文化附加到上下文中,但您并没有告诉上下文该对象实际上已经发生了变化。

我的机器上生成的代码是:

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

重要的是“AttachAsModified”。

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'.

浅唱々樱花落 2024-09-02 01:47:49

Timores 为我指出了正确的方向,解决方案(就我的问题而言)非常简单:只需将此方法添加到存储库中即可完成:

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

现在我们不再调用 Attach(),而是调用 AttachModified()。

谢谢帝汶人!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文