How to prevent Silverlight RIA Entity getting attached to datacontext before i'm ready

坚强是说给别人听的谎言 提交于 2019-12-04 10:01:01

You should be able to get the behavior you want by adding another 'MyProject' property to the TODO entity using a partial class. In your popup you could set the 'MyProject' property instead of the 'Project' property. When you save your TODO, you could then apply the 'MyProject' value directly to 'Project'. A little circuitous, but it should give you the behavior you'd like.

Kyle

I guess one way would be to use a PagedCollectionView and filter out 'New' entities - but there has to be something obvious you're missing.

    // data bind list to this ICollectionView
    private PagedCollectionView _projects;
    public PagedCollectionView Projects
    {
        get
        {
            if (_projects == null)
            {
                _projects = new PagedCollectionView(_todoDomainContext.TODOProjects)
                {
                    Filter = i => {

                        DM.TODOProject proj = (DM.TODOProject)i;

                        // hide New entities
                        if (proj.EntityState == EntityState.New)
                        {
                            return false;
                        }

                        return true;
                    }
                };
            }
            return _projects;
        }
    }

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.

What I was doing :

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

What the framework did by itself:

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

Why this is bad:

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

How i fixed it :

  • 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);
}

Have you tried using Context.Detach on the object so that you clearly specify that it shouldn't be in the context? Then you can Context.Attach it again before you save it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!