Copy & Clone of Entities

Posts   
 
    
csmith12
User
Posts: 33
Joined: 08-Dec-2003
# Posted on: 07-Dec-2004 05:12:15   

Experience members,

I know this question has been asked before. (I know I search for 3 hours) But time to ask again.

I am trying to copy a entity and it is not going very well for me. I have tried many of the methods that I found when I searched the forum, but nothing works and I get many different errors with each method.

MSSQL 2000 VB.NET 2003 LLBLGenPro 1.0.2003.1

The method I would like to use is below. - Produces errors on some date columns that I have in the table. I also read in the post that this will not produce a deep clone, but I added Customer.isDirty = True and then I get error on PK on related table when doing the customer save. You will also see that this table as many FK to other tables (status_id, vendor_id, ect....).


CREATE TABLE [dbo].[Design] (
    [Design_ID] [int] IDENTITY (1, 1) NOT NULL ,
    [Created] [datetime] NOT NULL ,
    [Enabled] [bit] NULL ,
    [Wave_ID] [int] NULL ,
    [StatusCode_ID] [int] NULL ,
    [DisabledDate] [datetime] NULL ,
    [DesignNumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [PriceGoal] [float] NULL ,
    [Vendor_ID] [int] NULL ,
    [PackLocation_ID] [int] NULL ,
    [DesignClass_ID] [int] NULL ,
    [DesignType_ID] [int] NULL ,
    [QTYShowSamples] [float] NULL ,
    [QTYSalesSamples] [float] NULL ,
    [QTYPOGSamples] [float] NULL ,
    [QTYCLRSamples] [float] NULL ,
    [RetailPrice] [float] NULL ,
    [PriceQuote] [float] NULL ,
    [PriceQuote2] [float] NULL ,
    [QTYOrderEstimate] [float] NULL ,
    [VendorItemNumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [CancelDate] [datetime] NULL ,
    [Group_ID] [int] NULL ,
    [Cancellor] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [Last_Wave_Before] [int] NULL ,
    [KitNumber] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [CasePack] [float] NULL ,
    [QuoteQTY] [float] NULL ,
    [QuoteQTY2] [float] NULL ,
    [CandyFill] [varbinary] (50) NULL ,
    [Account_ID] [int] NULL 
) ON [PRIMARY]
GO


Design.isNew = True
for I as Integer = 0 to Design.Fields.Count -1
  Design.Fields(I).isChanged = True
Next
Design.Save

Not exactly sure what is going on, can someone push me though this block?

Thanks,

Chris Smith

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 07-Dec-2004 10:08:45   

set design.Fields.IsDirty to true as well.

What is the method you used to clone the entity? Via the IClonable implementation of EntityFields?

( this: -> DesignEntity clone = new DesignEntity(); clone.Fields = ((EntityFields)source.Fields).Clone(); )

Or via a memorystream serialization/deserialization?

Frans Bouma | Lead developer LLBLGen Pro
csmith12
User
Posts: 33
Joined: 08-Dec-2003
# Posted on: 07-Dec-2004 16:16:00   

Well, I am not using any of those, I read a post that one could just reset the flags as my code above shows to make the existing entity perform an insert instead of update.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 07-Dec-2004 16:39:33   

ok, but the clone is then done in the database, not in memory. Be sure to set entity.Fields.IsDirty to true as well.

Frans Bouma | Lead developer LLBLGen Pro
csmith12
User
Posts: 33
Joined: 08-Dec-2003
# Posted on: 07-Dec-2004 17:33:00   

Thanks for you help, but still trying to make it through this issue.


    Try
      Design.IsNew = True
      For I As Integer = 0 To Design.Fields.Count - 1
        Design.Fields(I).IsChanged = True
      Next
      Design.Fields.IsDirty = True
      Design.Save()
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

Here is the error

Message "An exception was caught during the execution of an action query: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception." String

BSAPD avatar
BSAPD
User
Posts: 42
Joined: 02-Jul-2004
# Posted on: 07-Dec-2004 17:53:20   

You have to check to see if the database value was null, if it was you don't want to mark it as changed, because the actual value will be DateTime.MinValue which is invalid in SQL Server.

csmith12
User
Posts: 33
Joined: 08-Dec-2003
# Posted on: 07-Dec-2004 18:45:28   

I now have it working after adding the null check code.

Much thanks