 |
| 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
|
|
|
|

June 23rd, 2012, 08:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
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.
|
|

June 23rd, 2012, 10:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

June 23rd, 2012, 11:05 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
Yeah, I've been a bit apprehensive about testing it out!
__________________
Apocolypse2005, I'm a programmer - of sorts.
|
|

June 23rd, 2012, 11:13 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Why? You could create a simple demo project with a new clean database to see how it behaves...
Imar
|
|

June 23rd, 2012, 11:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
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.
|
|

June 23rd, 2012, 11:24 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

June 23rd, 2012, 12:03 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
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.
|
|

June 23rd, 2012, 02:15 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
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.
|
|

June 23rd, 2012, 02:21 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Have you tried removing the call to DetectChanges as I suggested in a previous post?
Imar
|
|

June 23rd, 2012, 02:31 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
|
|
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
|
|
 |