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
  #21 (permalink)  
Old June 23rd, 2012, 07:23 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

Your junction tables are missing primary keys. You need to select the two foreign keys and turn them into a (composite) primary key. Then you'll need to update or recreate the model.

I'll post a code sample tomorrow.

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
The Following User Says Thank You to Imar For This Useful Post:
Apocolypse2005 (June 23rd, 2012)
  #22 (permalink)  
Old June 23rd, 2012, 08:05 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

Cheers Imar, much appreciated, I was getting really worried about this but thanks for spending some time on it!
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #23 (permalink)  
Old June 24th, 2012, 05:05 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

Here's what worked for me:

1. Modified the database and made the two foreign keys columns in each junction tables a composite primary key (select both columns, then press the Primary Key button and save your changes.)

2. I recreated my model. Maybe updating works as well; haven't tried that out.

3. Wrote the following code that queries a Product, then creates a Variant, and finally creates a VariantProduct that associates the two. EF is able to see that a Prooduct is now related to a Variant through the VariantProduct entity and includes both (Variant and VariantProduct) in the context for you, so when you call SaveChanges, these two tables are updated for you. If you had also created a new Product, that would have been created as well).

Code:
// Query Product
var product = context.Products.First();
 
// Create new Variant
var variant = new Variant { 
  variantID = Guid.NewGuid(),
  variant_discount_on = 4,
  variant_markip_rate = 3,
  variant_total_cost = 4 
};
 
// Create new VariantProduct to associate the two
var variantProduct = new VariantProduct { 
  Product = product,
  Variant = variant,
  quantity = 100 
};
context.SaveChanges();
The context variable in my example is called ptx or _db in your example, and is an instance of the Entity Framework context.

Hope this helps,

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
  #24 (permalink)  
Old June 24th, 2012, 08:23 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

Code:
// Create new Variant 
var variant = new Variant {    
    variantID = Guid.NewGuid(),   
    variant_discount_on = 4,   
    variant_markip_rate = 3,   
    variant_total_cost = 4  
};   

// Create new VariantProduct to associate the two 
var variantProduct = new VariantProduct {    
    Product = product,   
    Variant = variant,   
    quantity = 100  
};
I don't know where this is added to the database, I can't see any assignments.

This has been so much help, the composite keys and that, I never knew! Trying to put my degree level Databases to good use. . . . not working to well :)

To create foreign keys, you just clikc add constraints then through that way?
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #25 (permalink)  
Old June 24th, 2012, 08:25 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:
I don't know where this is added to the database, I can't see any assignments.
You don't have to. Since Product already comes from the context, EF will understand that Variant and VariantProduct need to be added as well. If you were newing up the Product too, you would have to add it to the Products collection.

Quote:
To create foreign keys, you just clikc add constraints then through that way?
Or drag columns from 1 table to another in the diagram designer.

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
  #26 (permalink)  
Old June 24th, 2012, 08:29 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

So when you do this
Code:
 
var ptx = new GaleEntities();
And you start instantiating new objects from that entity, they automatically added anyway.

Code:
var newProduct = new Product();
that is automatically added because its done after instantiating the entity
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #27 (permalink)  
Old June 24th, 2012, 08:32 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

Oh yeah, and can you also tell me why my database is always being used by MSSQL Server process, its happened to a few of my DB files so i can never do anything with them again!

Haha I went on to SQL Management Studio and clicked delete, Close Existing Connection (the only box i made sure was ticked) now it's deleted them :)
__________________
Apocolypse2005, I'm a programmer - of sorts.

Last edited by Apocolypse2005; June 24th, 2012 at 08:36 AM.. Reason: Buggered up database
Reply With Quote
  #28 (permalink)  
Old June 24th, 2012, 08:40 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

Quote:
Originally Posted by Imar View Post

Or drag columns from 1 table to another in the diagram designer.

Imar
Wow the diagram designer is easy to use, I never knew you could do that :)
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #29 (permalink)  
Old June 24th, 2012, 08:48 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:
that is automatically added because its done after instantiating the entity
No, that's not how it works. That new Product would *not* be added to the context. The answer is in my previous posts:

Quote:
Since Product already comes from the context, EF will understand that Variant and VariantProduct need to be added as well. If you were newing up the Product too, you would have to add it to the Products collection.
Your code (or actually mine) queries a Product from the context. Since this is an EF-tracked entity, and not a normal Product it's able to analyze the relationships and bring it other related entities (such as the Variant and the VariantProduct) automatically. If you just new up a Product (such as in your last example) you would have to add at least one of the objects to the context for EF to be aware of them.

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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Apocolypse2005 (June 24th, 2012)
  #30 (permalink)  
Old June 24th, 2012, 08:53 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

Oh i get it! Thanks a lot, did you see my other posts about the database connections?

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

Also, in my post on stackoverflow, focussing on the multiplicities, how are those constraints created in the database? Especially Cascade deleting?
__________________
Apocolypse2005, I'm a programmer - of sorts.
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 09:00 AM.


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