Thanks Walaa.
I see the problem given the trace. My PK column is Identity (with automatic increment). The Entity hence does not have the PK when refetching.
I modified my method and it works now:
/// <summary>
/// Updates the publication locations.
/// </summary>
/// <param name="publicationLocations">The publication locations.</param>
public void UpdatePublicationLocations(EntityCollection<PublicationLocationEntity> publicationLocations) {
using (DataAccessAdapter adapter = new DataAccessAdapter()) {
adapter.StartTransaction(IsolationLevel.ReadCommitted, "UpdatePublicationLocations");
try {
EntityCollection<PublicationLocationEntity> oldState = this.SelectPublicationLocations();
adapter.SaveEntityCollection(publicationLocations, false, false);
SortedList<long, PublicationLocationEntity> newStateHashed = new SortedList<long, PublicationLocationEntity>();
foreach (PublicationLocationEntity publicationLocation in publicationLocations) {
if (publicationLocation.PublicationLocationId != 0) {
newStateHashed.Add(publicationLocation.PublicationLocationId, publicationLocation);
}
}
foreach (PublicationLocationEntity oldPublicationLocation in oldState) {
if (!newStateHashed.ContainsKey(oldPublicationLocation.PublicationLocationId)) {
adapter.DeleteEntity(oldPublicationLocation);
}
}
adapter.Commit();
} catch {
adapter.Rollback();
throw;
}
}
}
Method Enter: CreateInsertDQ
Method Enter: CreateSingleTargetInsertDQ
Generated Sql query:
Query: INSERT INTO [NOV_Reporting].[dbo].[PublicationLocation] ([Name], [Uri]) VALUES (@Name, @Uri)
Parameter: @Name : String. Length: 100. Precision: 0. Scale: 0. Direction: Input. Value: "Test Server".
Parameter: @Uri : String. Length: 250. Precision: 0. Scale: 0. Direction: Input. Value: "http://www.bytetec.no".
Method Exit: CreateSingleTargetInsertDQ
Method Exit: CreateInsertDQ
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query:
Query: SELECT [NOV_Reporting].[dbo].[PublicationLocation].[PublicationLocationId], [NOV_Reporting].[dbo].[PublicationLocation].[Name], [NOV_Reporting].[dbo].[PublicationLocation].[Uri] FROM [NOV_Reporting].[dbo].[PublicationLocation] WHERE ( ( [NOV_Reporting].[dbo].[PublicationLocation].[PublicationLocationId] = @PublicationLocationId1))
Parameter: @PublicationLocationId1 : Int64. Length: 0. Precision: 19. Scale: 0. Direction: Input. Value: <undefined value>.
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.