将linq中包含子实体的断开连接的实体更新为sql

bfl*_*mi3 5 c# linq-to-sql

在n层应用程序中,linq-to-sql似乎没有明确的解决方案来更新具有子EntitySet的断开连接的实体.

我有一些linq-to-sql实体......

public partial class Location : INotifyPropertyChanging, INotifyPropertyChanged
{       
    public int id;      
    public System.Nullable<int> idLocation;     
    public string brandingName;     
    public System.Data.Linq.Binary timeStamp;       
    public EntitySet<LocationZipCode> LocationZipCodes;
}

public partial class LocationZipCode : INotifyPropertyChanging, INotifyPropertyChanged
{
    public string zipcode;      
    public string state;        
    public int idLocationDetail;        
    public int id;      
    public System.Data.Linq.Binary timeStamp;       
    public EntityRef<Location> Location;
}
Run Code Online (Sandbox Code Playgroud)

因此,一个Location实体将拥有EntitySetLocationZipCodes.

所述Location领域模型被映射到表示层消耗,然后最终发送回在那里的映射回改变的视图模型实体视图模型Location域模型.从那里我更新实体并保存更改.这是处理程序:

public class ProgramZipCodeManagerHandler : IHttpHandler {
    private LocationsZipCodeUnitOfWork _locationsZipCodeUnitOfWork = new LocationsZipCodeUnitOfWork();

    public void ProcessRequest(HttpContext context) {
        if (context.Request.HttpMethod == "POST") {
            string json = Json.getFromInputStream(context.Request.InputStream);

            if (!string.IsNullOrEmpty(json)) {
                Location newLocation = Json.deserialize<Location>(json);
                if (newLocation != null) {
                    //this maps the location view model from the client to the location domain model
                    var newDomainLocation = new Mapper<Location, DomainLocation>(new DomainLocationMapTemplate()).map(newLocation);

                    if (newDomainLocation.id == 0)
                        _locationsZipCodeUnitOfWork.locationRepository.insert(newDomainLocation);
                    else
                        _locationsZipCodeUnitOfWork.locationRepository.update(newDomainLocation);

                    _locationsZipCodeUnitOfWork.saveChanges(ConflictMode.ContinueOnConflict);

                    var viewModel = new Mapper<DomainLocation, Location>(new LocationMapTemplate()).map(newDomainLocation);
                    context.Response.ContentType = "application/json";
                    context.Response.Write(Json.serialize(viewModel);
                }
            }
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的更新方法locationRepository:

protected System.Data.Linq.Table<T> _table;

public void update(T entity) {
    _table.Attach(entity, true);
    _context.Refresh(RefreshMode.KeepCurrentValues, entity);
}

public void update(T newEntity, T oldEntity) {
    _table.Attach(newEntity, oldEntity);
    _context.Refresh(RefreshMode.KeepCurrentValues, newEntity);
}
Run Code Online (Sandbox Code Playgroud)

我可以看到直接与Location实体关联的所有记录都在更新,但子集合(public EntitySet<LocationZipCode> LocationZipCodes)没有被更新.

是否有明确的方法来更新具有子EntitySet的断开连接的实体,该实体也需要更新?换句话说,我有一个分离的实体,它有另一个实体的集合.该集合已更改,我需要在数据库中更新它.

G.Y*_*G.Y 2

不..你不能那样做。

附加和分离对象是根据您使用的对象进行的,不会影响相关对象(实体)。

您可以从此处阅读更多信息:
附加和分离对象

考虑这样一种情况:对象 A 与包含 1000 个值的集合 B 相关。您分离 A 并将其发送到某个远程处理 - A 与 B 关系中的 null 一起发送。现在 A 被带回您的程序 - 无法知道 AB null 是远程处理的结果还是曾经存在它已经为空提供给远程处理。

在与此答案一起发布的链接中 - 请向下滚动以仔细阅读标题为:
分离对象的注意事项的部分。