准备好之前如何防止Silverlight RIA实体附加到datacontext [英] How to prevent Silverlight RIA Entity getting attached to datacontext before i'm ready

查看:73
本文介绍了准备好之前如何防止Silverlight RIA实体附加到datacontext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 4应用程序,用于简单的 TODO列表。我遇到的问题是数据绑定正在关联我的 TODO 对象上的关系,这导致RIA数据上下文将其添加到 DataContext中。待办事项列表,然后再在此显示。
我想将对象视为新对象并分离,直到我明确准备将其添加到数据上下文中。

I have a Silverlight 4 application for a simple 'TODO' list. The problem I'm having is that databinding is hooking up relationships on my TODO object, which causes the RIA data context to add it to the DataContext.TODOs list before I want it there. I want to treat the object as new and detached until I'm explicitly ready to add it to the datacontext.

这是它的工作方式:
我有与状态 TODO 实体>(RIA服务实体关系)。

Here's how it works : I've got my TODO entity which is associated with a Status (RIA services entity relationship).

我创建一个新的 TODO()实体,该实体将传递给 ChildWindow 弹出窗口。请注意,我没有将此新实体添加到我的数据上下文中。

I create a new TODO() entity which is passed to a ChildWindow popup. Notice that I don't add this new entity to my datacontext.

 new CreateTODOPopup(new TODO()).Show();

在ChildWindow的DataForm中,我有一个组合框,用于显示 Status 绑定到 DataContext.Statuses

In the DataForm in my ChildWindow I have a combobox for Status which is databound to DataContext.Statuses.

问题是选择<下拉菜单中的code> Status 实际上将实体与上下文相关联-最终为其赋予状态 EntityState.New 和实际上将其添加到 DataContext.TODOs 集合中。

The problem is that the action of selecting a Status from the dropdown actually associates the entity to the context for me - ending up giving it a state of EntityState.New and actually adding it to the DataContext.TODOs colleciton.

这很好,只不过它现在出现在主框架的主TODO列表中。我不希望这是因为尚未由ChildWindow提交。

This would be fine except that it now appears in the main TODO list in the main frame. I don't want this becasue it hasn't been committed by the ChildWindow yet.

我该如何解决?通过阻止实体连接-或以某种方式将其从绑定的任何控件中隐藏起来,直到添加该实体为止。

How can I solve this? Either by preventing the entity from becoming attached - or by somehow hiding it from any controls it is bound to until it has been added.

推荐答案

重要提示:经过3天的可怕种族类型条件苦苦挣扎之后,事实证明这与该问题直接相关。基本上得出的结论是-如果您尝试创建实体并且不将其添加到数据上下文中,那么如果您不想要竞争条件和意外行为,请不要。我很确定这是一个RIA服务错误。

IMPORTANT: After 3 days of struggling with a horrible race type condition it turned out to be directly related to this problem. Basically the conclusion is - if you're trying to create entities and not add them to the data context then don't if you don't want race conditions and unexpected behavior. I'm pretty sure this is a RIA services bug.

我在做什么:


  • 创建一个新的TODO()并将其传递给我的视图

  • 允许RIA服务框架关联我的 TODO 和所有外键表,例如 Status AssignedTo

  • 保存时,将 TODO()添加到列表中(如果 DataContext.TODOs 实体集。

  • Creating a new TODO() and passing it to my view
  • Allowing the RIA services framework to associate my TODO with all the foreign key tables, such as Status and AssignedTo
  • On 'save' adding the TODO() to the list if it wasn't already present in the DataContext.TODOs entity set.

框架本身做了什么:


  • 当视图在对象上设置外键(通过组合框)时,它将自动添加 TODO DataContext.TODOS 集合中。这就是实体的工作方式。

  • When the View sets the foreign keys on the object (via a comobox) it would automatically add the TODO to the DataContext.TODOS collection. This is just the way entities work.

为什么这样不好:


  • 在我的UI中导航时,发生了一些可怕的比赛情况。

  • 先前存在的行(甚至是在我的应用启动之前已经存在的行)都被标记为 New -有时多达20行,然后得到重新保存为新的重复行。

  • When navigating around my UI some horrible race condition was occuring.
  • Preexisting rows (even those that existed before my app started) were marked as New - sometimes up to 20 of them and then getting resaved as new duplicate rows.

我如何修复它:


  • 总是在创建时立即将创建的实体添加到数据上下文中。

  • Always add created entities to the data context immediately on creation.

这里有一些用于在创建时立即添加实体的示例代码-无竞争条件:

Here's some sample code for adding entities immediately on creation - no race condition:

for (int i = 0; i < 3; i++) 
{
    var entity = new DM.TODO();
    _todoDomainContext.TODOs.Add(entity);

    entity.TODOStatu = pendingStatus;
    entity.TODOProject = project;
    entity.TODOCompany = company;

    entity.CreateDt = DateTime.Now;

    entity.Title = "generated todo " + DateTime.Now.ToString();
    entity.Details = "12345"; 
}

此代码无效,并导致竞争条件-在外键后添加实体约束已经设置:

This code did NOT work, and causes race conditions - adding entities after foreign key constraints have already been set:

for (int i = 0; i < 3; i++) 
{
    var entity = new DM.TODO();

    entity.TODOStatu = pendingStatus;
    entity.TODOProject = project;
    entity.TODOCompany = company;

    entity.CreateDt = DateTime.Now;

    entity.Title = "generated todo " + DateTime.Now.ToString();
    entity.Details = "12345"; 

    _todoDomainContext.TODOs.Add(entity);
}

这篇关于准备好之前如何防止Silverlight RIA实体附加到datacontext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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