n層アプリケーションでは、linq-to-sqlには、子エンティティセットを持つ切断されたエンティティを更新するための明確な解決策がないようです。linqからsqlへの子エンティティを持つ切断されたエンティティの更新
私はそうLocation
エンティティがLocationZipCodes
のEntitySet
を持っているでしょう
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;
}
...いくつかのLINQからSQLへのエンティティを持っています。
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);
}
}
}
}
}
私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);
}
私が直接更新されているLocation
エンティティが、子コレクション(public EntitySet<LocationZipCode> LocationZipCodes
)に関連付けられたすべてのレコードがあることがわかります。ここハンドラです更新されていません。
更新が必要な子EntitySetを持つ切断されたエンティティを更新する明確な方法がありますか?つまり、別のエンティティのコレクションを持つ分離されたエンティティがあります。そのコレクションは変更されており、データベース内のコレクションを更新する必要があります。
子コレクションのエントリにそれぞれのIDがありますか? – rhughes
はい彼らは自分のIDを持っています – bflemi3