域服务-服务器端更新

发布于 2024-11-07 15:48:58 字数 229 浏览 0 评论 0原文

我有 silverlight 4 RIA 域服务。在一种特定方法中,我需要更改数据库中的一个值(当然还有其他事情)。为此,我获取实体,更改一个值,然后我需要将该更改保存回数据库。

我尝试调用实体的生成更新函数,该函数仅调用 this.ObjectContext.myEntity.AttachAsModified(myENtity);但更改永远不会返回到数据库。

如何从服务器端保存值(即客户端从未有过此数据)?

I have a silverlight 4 RIA Domain Service. In one particular method I need to change one value in the database (among other things of course). To do this I get the entity, change the one value, then I need to save that change back to the DB.

I've tried calling the entity's generated update function, which just calls this.ObjectContext.myEntity.AttachAsModified(myENtity); but the change never gets back to the database.

How do I save values from the sever side (ie. the client never had this data)?

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

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

发布评论

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

评论(2

記憶穿過時間隧道 2024-11-14 15:48:58

您应该知道 UpdateXXX 方法实际上并不将更改提交到数据库 - 只是稍后才会发生。知道了这一点,我们可以将 UpdateXXX 方法的默认实现更改为:(我假设 XXX == Product 这里)

    public void UpdateProduct(Product currentProduct)
    {
        this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
    }

    public void UpdateProduct(Product currentProduct)
    {
        // This line only reattach the entity back to EF context, it doesn't submit changes yet
        this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));

        currentProduct.SomeProperty = "SomeChange"; // This change is going to be submitted later
    }

You should know that the UpdateXXX method doesn't actually submit the changes to the database - that only happens a bit later. knowing this, we can change the default implementation of the UpdateXXX method: (I'm assuming XXX == Product here)

    public void UpdateProduct(Product currentProduct)
    {
        this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
    }

to

    public void UpdateProduct(Product currentProduct)
    {
        // This line only reattach the entity back to EF context, it doesn't submit changes yet
        this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));

        currentProduct.SomeProperty = "SomeChange"; // This change is going to be submitted later
    }
挽容 2024-11-14 15:48:58

事实证明,在更改之前或之后附加对象没有什么区别。
缺少的部分是:

this.ObjectContext.SaveChanges();

Turns out, attaching the object before or after the change makes no difference.
The missing peice was :

this.ObjectContext.SaveChanges();

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