简体   繁体   中英

Do Entity Framework Relationship Properties Update?

I have some code like this in a WCF web method,

    List<LocationInRoad> locationInRoad = new List<LocationInRoad>();

    foreach (CarWorkLocationLink locationLink in source.CarWorkLocationLinks)
    {
         locationInRoad.Add(LocationInRoadMapper.MapTo(locationLink.CarWorkLocationType.WorkLocationTypeID));
    }

    destination.LocationInRoad = locationInRoad.ToArray();

Sometimes (perhaps once a week) in production an error occurs,

InvalidOperationException has occured  Message: Collection was modified; enumeration operation may not execute.

So it seems to be telling me that the 'source.CarWorkLocationLinks' collection has been modified part way through enumerating the list in the foreach loop.

So to explain, 'source' is an entity framework entity loaded from our database and 'CarWorkLocationLinks' is defined on that entity like this,

    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("CarManagerModel", "FK_CarWorkLocationLink_CarDetail", "CarWorkLocationLink")]
    public EntityCollection<CarWorkLocationLink> CarWorkLocationLinks
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<CarWorkLocationLink>("CarManagerModel.FK_CarWorkLocationLink_CarDetail", "CarWorkLocationLink");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<CarWorkLocationLink>("CarManagerModel.FK_CarWorkLocationLink_CarDetail", "CarWorkLocationLink", value);
            }
        }
    }

Ie it is an entity relationship to another table. So I guess the question is can an 'EntityCollection' be modified after it is loaded if something in the database changes?

So basically overall the code above fits into the WCF call like this,

public APIEntity WCFCall(parameters)
{
    using (EntityContext context = new EntityContext())
    {
        // loading entity (database entity that is)
        // creating API entity (this is a POCO object to control what is exposed over the WCF service)
        // running the loop as shown above on the 'loaded entity' and popluating fields in the poco object
        // returning the poco object
    }
}

So I am not sure why the error I mentioned should occur. Is is self contained.

If you are using shared context (you are not creating ObjectContex per request / unit of work) then the answer is yes and the explanation is here - ObjectContext creates for each record identified by primary key only one entity instance and this instance is reused for each subsequent requests (it is called Identity map pattern). So if you share the context and you have multithreaded application (like web service, asp.net, etc.) all threads can concurrently use the same entity instance and modify same collection of related objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM