Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架关系属性是否更新?_C#_.net_Entity Framework - Fatal编程技术网

C# 实体框架关系属性是否更新?

C# 实体框架关系属性是否更新?,c#,.net,entity-framework,C#,.net,Entity Framework,我在WCF web方法中有一些类似的代码 List<LocationInRoad> locationInRoad = new List<LocationInRoad>(); foreach (CarWorkLocationLink locationLink in source.CarWorkLocationLinks) { locationInRoad.Add(LocationInRoadMapper.MapTo(locatio

我在WCF web方法中有一些类似的代码

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

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

    destination.LocationInRoad = locationInRoad.ToArray();
因此,它似乎告诉我,'source.CarWorkLocationLinks'集合已经通过枚举foreach循环中的列表进行了部分修改

因此,为了解释,“source”是从数据库加载的实体框架实体,“CarWorkLocationLinks”是在该实体上定义的,如下所示:

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

所以我不确定为什么会出现我提到的错误。是自包含的。

如果您使用的是共享上下文(您没有为每个请求/工作单元创建
ObjectContex
),那么答案是肯定的,-
ObjectContext
只为主键标识的每个记录创建一个实体实例,并且该实例将在后续的每个请求中重用(称为标识映射模式)。因此,如果您共享上下文,并且您有多线程应用程序(如web服务、asp.net等),则所有线程都可以同时使用同一实体实例并修改相同的相关对象集合。

如果您使用共享上下文(您没有为每个请求/工作单元创建
ObjectContex
),那么答案是“是”,并且-
ObjectContext
只为主键标识的每个记录创建一个实体实例,并且该实例将在每个后续请求中重复使用(称为标识映射模式)。因此,如果您共享上下文,并且有多线程应用程序(如web服务、asp.net等)所有线程都可以同时使用相同的实体实例并修改相同的相关对象集合。

好的,谢谢这让我思考。但我不确定这是否100%解释了它。我将更新我的问题,因为我没有根据您所说的内容解释完整的上下文。这将使用每次调用的上下文,因此不应该有任何上下文如果你不在代码中做任何嵌套线程,就会产生干扰。我唯一能想象的是延迟加载和修复实体图的一些奇怪行为。好的,谢谢,这真的让我思考了。但我不确定这能解释100%。我会更新我的问题,因为我没有根据你所说的解释完整的上下文。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
    }
}