Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 23rd, 2012, 08:58 AM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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.
Reply With Quote
  #2 (permalink)  
Old June 23rd, 2012, 10:49 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old June 23rd, 2012, 11:05 AM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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.
Reply With Quote
  #4 (permalink)  
Old June 23rd, 2012, 11:13 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #5 (permalink)  
Old June 23rd, 2012, 11:16 AM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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.
Reply With Quote
  #6 (permalink)  
Old June 23rd, 2012, 11:24 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #7 (permalink)  
Old June 23rd, 2012, 12:03 PM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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.
Reply With Quote
  #8 (permalink)  
Old June 23rd, 2012, 02:15 PM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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.
Reply With Quote
  #9 (permalink)  
Old June 23rd, 2012, 02:21 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #10 (permalink)  
Old June 23rd, 2012, 02:31 PM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:39 PM.


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