Solved

Copy related entities

Posted on 2013-01-20
3
927 Views
Last Modified: 2013-02-28
Hi I have some entities e.g Customers, OrderHeads, Orderlines
Customers
CustomerId
Name
VersionNo

OrderHeads
OrderId
CustomerId
Date

OrderLines
OrderLineId
OrderNo
ProductId
Qty

All the pk ids are identities.
I want to "version" a customer i.e take a copy of the customers data and all related data.

I am trying something similare to this.
 currentCustomer = Context.Customers.Include("OrderHeads").Include("OrderHeads.OrderLines")
                   .Where(c => c.CustomerId == 5).SingleOrDefault();

Customers newCust = currentCustomer ;
 
db.Project.Add(newProject);
db.SaveChanges();

Open in new window


This does copy the customer and the newCustomer has a new Id in the table. But then I get an error.

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.\r\n   at System.Data.Objects.ObjectContext.SaveChanges


It has copied the customer but not the Orders.
Is what I am trying to do possible and if so how. (I would rather not have to loop through each child changing the customerId)

i tried it with these to classes also but got the same error

 public class Test
    {
        
        public virtual ICollection<TestChild> TestChild { get; set; }
        public int TestId { get; set; }

        public int tno { get; set; }
       
            }

    public class TestChild
    {
        
        public virtual Test test { get; set; }
        
        //FK
        public int TestId { get; set; }


        public int TestChildId { get; set; }
        public int childno { get; set; }
    }

Open in new window

                   var test = db.Test.Include("TestChild")
                   .Where(p => p.TestId == 2).SingleOrDefault();
                    if (test != null)
                    {

                        Test newTest = test;
                        //newProject.VersionNo += 1;

                        db.Test.Add(newTest);
                        db.SaveChanges();

Open in new window

0
Comment
Question by:Eamon
3 Comments
 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
ID: 38799745
Dumb thought -- but would a stored procedure work better and then returning the results to the C# interface?
0
 
LVL 1

Author Comment

by:Eamon
ID: 38800121
Originally I had a SP for versioning but am trying to do everything in entity framework code first. Also with the SP if you change the table e.g. add a new field you have to update the SP.
0
 
LVL 25

Expert Comment

by:apeter
ID: 38803342
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question