Solved

Copy related entities

Posted on 2013-01-20
3
936 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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