Solved

Copy related entities

Posted on 2013-01-20
3
914 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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now