Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2012, 08:58 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default Entity Framework Complex Relationships

I have a complex relationship in my Database.
Code:
Packages (packageID, package_name , .... )
VariantPackage (packageID, variantID)
Variant (variantID, variant_name, .... )
ProductVariant (variantID, productID)
Product (productID, product_name, .... )
(I take it you can assume the ID fields are Primary or Foreign respectively?)
The entity framework has elegantly picked up on this because if do this:
Code:
Package newPackage = new Package()
{
 packageID = Guid.NewGuid()
 etc . . .
};

Variant newVariant = new Variant()
{
 variantID = Guid.NewGuid()
 etc . . .
};
I get the option when doing this line below to add the variant to that package
Code:
newPackage.Variant.AddObject(newVariant);
_db.Package.AddObject(newPackage);
_db.SaveChanges();
Now when I SaveChanges will that in those 2 lines, create entries in all 3 tables? If I have set up the model correctly?
Thanks
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 23rd, 2012, 10:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Now when I SaveChanges will that in those 2 lines, create entries in all 3 tables? If I have set up the model correctly?
Why don't you try it out? Easiest way to tell....

It should do it, provided the VariantPackage only has the two mentioned foreign keys.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2012, 11:05 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Yeah, I've been a bit apprehensive about testing it out!
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 23rd, 2012, 11:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Why? You could create a simple demo project with a new clean database to see how it behaves...

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2012, 11:16 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Is it allowed to DetectChanges() and SaveChanges() after all the creation of the products and variants and package has been done?
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 23rd, 2012, 11:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If this is a Database First model, you shouldn't have to call DetectChanges as SaveChanges will figure that out for you.

Can you be more explicit about what you're asking and why? Is it not working?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2012, 12:03 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

I'm just about to test to see if it works. I'm just a tad new to this entity framework stuff, I've got a book under my nose and I was just wondering if my theory was correct!
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 23rd, 2012, 02:15 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default Error When Doing it

I get this error after executing the SaveChanges(); (this is preceded by DetectChanges();)
Code:
An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 23rd, 2012, 02:21 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried removing the call to DetectChanges as I suggested in a previous post?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 23rd, 2012, 02:31 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

It doesn't get that far, it does this:

Code:
 foreach (object[] idQuant in prodList)
            {
                Guid prodId = Guid.Parse((string)idQuant[0]);
                newVariant.VariantProducts.Add(new VariantProduct(){
                    variantID = newVariant.variantID,
                    productID = prodId,
                    quantity = (int)idQuant[1]
                });
            }

            Guid packageID =  Guid.Parse(AddPackageList.SelectedValue);
            // 081-0f46219e1c0d
            IQueryable<Package> result = ptx.Packages.Where(o => o.packageID == packageID);
            Package firstOne = result.First<Package>();
            firstOne.Variants.Add(newVariant); 
            ptx.Packages.AddObject(firstOne); //GETS HERE its because there is already a package object in the database, i just want to update it
__________________
Apocolypse2005, I'm a programmer - of sorts.

Last edited by Apocolypse2005; June 23rd, 2012 at 02:33 PM.. Reason: FOUND THE ERROR





Similar Threads
Thread Thread Starter Forum Replies Last Post
Having problems with Entity Framework Rushino ASP.NET 4 General Discussion 7 June 12th, 2013 05:17 PM
Entity Framework Question nanonerd BOOK: Beginning ASP.NET 4 : in C# and VB 3 January 15th, 2012 07:59 AM
Entity Framework And MVC geomar BOOK: Professional ASP.NET MVC 1.0 ISBN: 978-0-470-38461-9 5 January 24th, 2011 04:58 PM
Entity Framework vs datasets? hoss BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 30th, 2010 02:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.