SaveEntityCollection with refetch, ORMEntityOutOfSyncException

Posts   
 
    
hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 22-Apr-2007 13:27:30   

Dear all,

I have implemented a basic method to update a table. I save an EntityCollection with the refetch flag raised. The collection does however not get refetched. Please see attached image. What am I doing wrong?


/// <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, true, false);
            SortedList<long, PublicationLocationEntity> newStateHashed = new SortedList<long, PublicationLocationEntity>();
            foreach (PublicationLocationEntity publicationLocation in publicationLocations) {
                newStateHashed.Add(publicationLocation.PublicationLocationId, publicationLocation);
            }
            foreach (PublicationLocationEntity oldPublicationLocation in oldState) {
                if (!newStateHashed.ContainsKey(oldPublicationLocation.PublicationLocationId)) {
                    adapter.DeleteEntity(oldPublicationLocation);
                }
            }
            adapter.Commit();
        } catch {
            adapter.Rollback();
            throw;
        }
    }
}

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 22-Apr-2007 13:36:14   

I forgot to enter version information: 2.0.0.0 Final: December 6th, 2006

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Apr-2007 21:29:30   

Hi hotchill,

In the image I can see two breakpoints, but the image was captured when the trace point is out of scope. Could you confirm if the outOfSync trows at the fisrt breakpoint?

David Elizondo | LLBLGen Support Team
hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 23-Apr-2007 10:13:59   

Hi daelmo simple_smile

You don't have to take my word for it. Myself, I don't even trust my own word when it comes to code wink I have attached another image.

Even if I do an explicit FetchEntityCollection on PublicationLocationEntity with null as predicate bucket after the SaveEntityCollection, I get the out of sync issue.

Thanks.

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 23-Apr-2007 10:25:51   

I don't know if it is needed, but here is my (very simple) table:


/****** Object:  Table [dbo].[PublicationLocation]  Script Date: 04/23/2007 10:18:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PublicationLocation](
    [PublicationLocationId] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](100) COLLATE Danish_Norwegian_CI_AS NOT NULL,
    [Uri] [nvarchar](250) COLLATE Danish_Norwegian_CI_AS NOT NULL,
 CONSTRAINT [PK_PublicationLocation] PRIMARY KEY CLUSTERED 
(
    [PublicationLocationId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [CT_PUBLICATIONLOCATION_NAME_UNIQUE] UNIQUE NONCLUSTERED 
(
    [Name] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Apr-2007 10:46:07   

Would you please check the generated/executed SQL queries and see if this particular entity was saved and refetched from the database or not? ref: manual "Using the generated code -> Troubleshooting and debugging"

Would you also try to refetch the collection after the commit and see if this makes a difference?

hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 23-Apr-2007 13:32:46   

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.