Solved

Copy related entities

Posted on 2013-01-20
3
941 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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