2
我插入与现有父记录关联的子记录。我如何刷新父记录以显示所有内容,包括新插入的子记录?将子记录插入linq到sql后刷新实体
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到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);
}