UnitofWork performance order

Posts   
 
    
JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 16-Mar-2007 18:07:15   

Using the code below, the AddDeleteEntitiesDirectlyCall action is performed AFTER the AddCollectionForSave call, kind of defeating the purpose of cleaning up before adding entities.

I know I could retrieve the DiskFileFlashCardTextElapsedTimeDataFields entities and delete them via a collection, but why should I have to?


private static void UpdateElapsedTime(DataTable dt)
{
    UnitOfWork2 uow = new UnitOfWork2();

    ArrayList fcidlist = new ArrayList();
    for (int i = 0; i < dt.Rows.Count; i++)
        fcidlist.Add(dt.Rows[i][DiskFileFlashCardTextFields.FlashCardSampleId.Name]);
    RelationPredicateBucket bucket =
        new RelationPredicateBucket(DiskFileFlashCardTextElapsedTimeDataFields.FlashCardSampleId == fcidlist);
    uow.AddDeleteEntitiesDirectlyCall("DiskFileFlashCardTextElapsedTimeDataEntity", bucket);
    
    EntityCollection<DiskFileFlashCardTextElapsedTimeDataEntity> etcollection =
        new EntityCollection<DiskFileFlashCardTextElapsedTimeDataEntity>(
            new DiskFileFlashCardTextElapsedTimeDataEntityFactory());

    // start at second row
    for (int i = 1; i < dt.Rows.Count; i++)
    {
        DiskFileFlashCardTextElapsedTimeDataEntity entity = etcollection.AddNew();
        entity.FlashCardSampleId = (Int64) dt.Rows[i][DiskFileFlashCardTextFields.FlashCardSampleId.Name];
        DateTime previoustime = (DateTime) dt.Rows[i-1][DiskFileFlashCardTextFields.SampleDate.Name];
        DateTime currenttime = (DateTime)dt.Rows[i][DiskFileFlashCardTextFields.SampleDate.Name];
        entity.CalcEt = Convert.ToInt32(currenttime.Subtract(previoustime).TotalMinutes);
        entity.Etactual = Convert.ToInt32(entity.CalcEt);
        entity.EndEdit();
    }
    uow.AddCollectionForSave(etcollection);

    using (DataAccessAdapter adapter = new DataAccessAdapter())
    {
        uow.Commit(adapter);
    }
}

LLBLGen 2.0 Designer 2007-02-14 Runtime Lib 2.0.7.129 SQL Server 2005 sp2 VS 2005 sp1


-- MILLIONS of records
CREATE TABLE [dbo].[DiskFileFlashCardText](
    [FlashCardSampleID] [bigint] IDENTITY(1,1) NOT NULL,
    [SampleDate] [datetime] NOT NULL
-- sample data columns removed
 CONSTRAINT [PK_DiskFileFlashCardText] PRIMARY KEY CLUSTERED 
(
    [FlashCardSampleID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

-- [DiskFileFlashCardTextElapsedTimeData] records only exist in certain cases
-- one to one relationship if record exists.
CREATE TABLE [dbo].[DiskFileFlashCardTextElapsedTimeData](
    [FlashCardSampleID] [bigint] NOT NULL,
    [CalcEt] [int] NULL,
    [ETActual] [int] NOT NULL
 CONSTRAINT [PK_DiskFileFlashCardTextElapsedTimeData] PRIMARY KEY CLUSTERED 
(
    [FlashCardSampleID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[DiskFileFlashCardTextElapsedTimeData]  WITH CHECK ADD  CONSTRAINT [FK_DiskFileFlashCardTextElapsedTimeData_DiskFileFlashCardText] FOREIGN KEY([FlashCardSampleID])
REFERENCES [dbo].[DiskFileFlashCardText] ([FlashCardSampleID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[DiskFileFlashCardTextElapsedTimeData] CHECK CONSTRAINT [FK_DiskFileFlashCardTextElapsedTimeData_DiskFileFlashCardText]

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Mar-2007 01:59:28   

I think you may have to because the order used is to ensure that there are no FK violations. Here's a post that was about the same question. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=6694&HighLight=1 Your workaround seems to be nearly the same suggestion that was given in this case also.