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 24th, 2012, 09:00 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

SQL locks mdf files, so I guess this is by design.

>> So if it wasn't tracked, I would need to do what i was earlier!

Yes, you need to add them to the context manually.

>> how are those constraints created in the database? Especially Cascade deleting?

I would again recommend getting a good book on EF as the one from Julie Lerman. The rules for this are pretty complex so you need a better understanding of how this works in order to fully use the potential of EF.

Short story: EF detects relationships between tables and sets up the proper Delete behavior for you. You can see what's set up by looking at the properties of a relationship in the EF designer.

Cheers,

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 24th, 2012, 09:08 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Quote:
Originally Posted by Imar View Post

I would again recommend getting a good book on EF as the one from Julie Lerman. The rules for this are pretty complex so you need a better understanding of how this works in order to fully use the potential of EF.
Am so on it! Need that book!

Code:
var ptx = new GaleEntities();
            var newVariant = new Variant()
            {
                variantID = Guid.NewGuid(),
                variant_name = VariantTitleText.Text,
                variant_number_of_people = noPeople,
                variant_discount_on = discounton,
                variant_discount_perc = strippedDisRate,
                variant_markup = strippedMarkRate,
            };
            Guid packageID = Guid.Parse(AddPackageList.SelectedValue);

            HashSet<object[]> prodList = new HashSet<object[]>();
            foreach (GridViewRow row in ProdGrid.Rows)
            {
                if (((CheckBox)row.Cells[4].Controls[0]).Checked)
                {
                    object [] prodIDQuant = new object[2];
                    prodIDQuant[1] = int.Parse(((TextBox)row.Cells[3].Controls[0]).Text);
                    prodIDQuant[0] = row.Cells[0].Text;
                    prodList.Add(prodIDQuant);
                 }
            }
            foreach (object[] idQuant in prodList)
            {
                Guid prodId = Guid.Parse((string)idQuant[0]);
                ptx.VariantProducts.AddObject(new VariantProduct()
                {
                    variantID = newVariant.variantID,
                    productID = prodId,
                    quantity = (int)idQuant[1]
                });
            }

            ptx.Variants.AddObject(newVariant);
            ptx.Packages.First(o => o.packageID == packageID).Variants.Add(newVariant);
I'm trying to that above, is it correct, I'm not sure how to add Variant to the Package, because the EF doesnt create an entity of the joining table for PackageVariant, is that because i select the option Pluralise, Singularise the tables? Is it a Problem?
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 24th, 2012, 09:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I have no idea what this code is trying to accomplish, nor do I understand your questions.

EF *does* create an entity set for the junction tables. It should be called PackageVariants.

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!

Last edited by Imar; June 24th, 2012 at 09:13 AM..
 
Old June 24th, 2012, 09:34 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

It doesn't funnily enough! However I have found some tutorial videos done by Julie Lerman, and I'm getting there!! Its very well described!

Thankyou for you kickstart and direction, very much appreciated!
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 25th, 2012, 04:21 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

I have tried to delete a package from this database.

It won't let me due to the foreign key constraint as it references the package in a different table.

So how do I setup a Cascade Delete Option?
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old June 25th, 2012, 06:10 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Get the book I recommended. It's all in there.

Short version: Set Cascade on the relationships in the EF diagram. For each cascade in EF, you also need to set one up in the database. EF only deletes objects it has in memory so you need to have the database delete any related records that EF may miss through a database cascade.

Alternatively, manually delete the related objects using DeleteObject on the EF context before you delete a primary entity.

Cheers,

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 25th, 2012, 08:07 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

Quote:
Originally Posted by Imar View Post
Get the book I recommended. It's all in there.
It's on its way, this is a meanwhile thing.

Quote:
Originally Posted by Imar View Post
Short version: Set Cascade on the relationships in the EF diagram. For each cascade in EF, you also need to set one up in the database. EF only deletes objects it has in memory so you need to have the database delete any related records that EF may miss through a database cascade.
I've tried to set one up in the EF but it doesn't like it. I have now managed to setup cascade deletes in the database, i have refreshed the entity model, do I have to setup any further cascade deletes in EF now I've done it on the database?

I have tried running it with just cascade done in the database but it hasn't propagated through the database, I didnt get an error though this time! I will check my cascade deletes but I already believe them to be correct!

I have tried adding OnDelete:Cascade in the EF however because there is no entity for the joining table between PackageEnt and Variant they both have multiplicity of * and you can't have a cascade delete when the ends are *. The joining table is not created as an entity because there are no other columns that are not shared by either table, because the foreign keys are it seems to leave out creating an entity because it has all the data it needs. The proof of what I have just said lies within the joining table for Variant and Product, because I ahve added an extra column in VariantProduct called Quantity it needs mapping data therefore the entity has to be created so a mapping can occur. So how on earth do I get EF to recognise the joining table PackageVariant without adding an extra column that is always going to be null? Or is that the solution?

Quote:
Originally Posted by Imar View Post
Alternatively, manually delete the related objects using DeleteObject on the EF context before you delete a primary entity.
So you mean, go to the Variants delete them then finally delete the package itself. Because I assume you have to use DeleteObject to delete anything from the EF
__________________
Apocolypse2005, I'm a programmer - of sorts.

Last edited by Apocolypse2005; June 25th, 2012 at 08:31 PM.. Reason: Found out how to do cascade delete on database, and tried it out, but it failed
 
Old June 26th, 2012, 09:53 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

>> do I have to setup any further cascade deletes in EF now I've done it on the database?

Yes, the recommendation is to have Cascades on both ends, or nowhere. So, refresh or recreate the model and make sure each relationship in EF is set up for Cascades as well.

>> because there is no entity for the joining table

I think that's a mistake. it's there when I *created* a new model with your database after I set the primary keys.

>> So you mean, go to the Variants delete them then finally delete the package itself.

You can query a package and then loop over its VariantPackages and delete those....

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!





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.