在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实体将拥有EntitySet的LocationZipCodes.
所述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的断开连接的实体,该实体也需要更新?换句话说,我有一个分离的实体,它有另一个实体的集合.该集合已更改,我需要在数据库中更新它.