tags:

views:

68

answers:

3

Hi there, In the legacy system I look after I have to add the same record to a table, twice.

There is an identity on the table that increments with each new record.

If I do the following I get the error: "Cannot add an entity that already exists"

Is there a way to add a record twice without creating another PricingSnapshot object?

            PricingSnapshot pricingSnapshot = new PricingSnapshot
            {
                InvoiceOrderLineId = invoiceOrderLineId,
                GrossPaymentValue = grossTotal,
                CreatedBy = "Pricing Engine",
                CreatedDate = DateTime.Now
            };
            dBpostChanges.PricingSnapshots.InsertOnSubmit(pricingSnapshot);
            dBpostChanges.SubmitChanges();

     dBpostChanges.PricingSnapshots.InsertOnSubmit(pricingSnapshot);
            dBpostChanges.SubmitChanges();
+3  A: 

Creating another Pricing Snapshot object is the "correct" way to do it. It's the way Linq to SQL was designed, so doing anything else would be swimming against the current.

Robert Harvey
A: 

I guess one column in your table is unique. Can you show your table schema?

+2  A: 

You will have to create a new PricingSnapshot object since after you submit changes the first time, your object pricingSnapshot still exists and now has been assigned an ID from your table. So if you were to try to access the ID of the object after the first submit, you will find it has one, therefore adding it to the table again, will be adding a duplicate.

Pavol