Solved

Copy related entities

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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