2

既存の親レコードに関連付けられている子レコードを挿入しています。親レコードを更新して、新しく挿入された子レコードを含むすべてを表示するにはどうすればよいですか?

context.Refresh(RefreshMode.OverwriteCurrentValues, entity)動作していません。

私の試みのより完全な例:

Location newLocation = Json.deserialize<Location>(json);
if (newLocation != null) {
    IEnumerable<string> zipCodes = Regex.Split(newLocation.zipCodes, @"[\s,;]+");

    // this verifies the new zipcodes against a table of all US zipcodes and returns matches 
    var newLocationZipCodes = _zipCodeRepository.match(zipCodes).Select(item => new LocationZipCode { idLocation = newLocation.id, state = item.state, zipcode = item.zipcode });

    // get the parent entity
    var domainLocation = _unitOfWork.locationRepository.getFirst(l => l.id == newLocation.id);

    // insert child entities
    if (newLocationZipCodes.Any()) {
        _unitOfWork.locationZipCodeRepository.insertAll(newLocationZipCodes);
        _unitOfWork.saveChanges(ConflictMode.ContinueOnConflict);
    }

    // this isn't working
    _unitOfWork.refresh(RefreshMode.OverwriteCurrentValues, domainLocation);
    return domainLocation;
}

以下は、linq-to-sql によって作成された LocationZipCode クラスの基本的な表現です。

public class LocationZipCode {
    int idLocation;
    string zipcode;
    string state
    EntityRef<Location> location;
}

そして、これが私の UnitOfWork での更新方法です。

public void refresh(RefreshMode refreshMode, object entity) {
    _context.Refresh(refreshMode, entity);
}
4

1 に答える 1

1

コンテキストを更新する代わりに、子レコードをデータベースに挿入する方法を変更しました。代わりに...

_unitOfWork.locationZipCodeRepository.insertAll(newLocationZipCodes);

私はこれをやっています...

domainLocation.LocationZipCodes.AddRange(newLocationZipCodes);

更新されたコードは次のようになります...

Location newLocation = Json.deserialize<Location>(json);
if (newLocation != null) {
    IEnumerable<string> zipCodes = Regex.Split(newLocation.zipCodes, @"[\s,;]+");

    var newLocationZipCodes = _zipCodeRepository.match(zipCodes).Select(item => new LocationZipCode { idLocation = newLocation.id, state = item.state, zipcode = item.zipcode });
    var domainLocation = _unitOfWork..locationRepository.getFirst(l => l.id == newLocation.id);

    if (newLocationZipCodes.Any()) {
        domainLocation.LocationZipCodes.AddRange(newLocationZipCodes);
        _unitOfWork.saveChanges(ConflictMode.ContinueOnConflict);
    }

    return new Mapper<DomainLocation, Location>(new LocationMapTemplate()).map(domainLocation);
}
于 2013-02-20T18:43:30.093 に答える