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
  #11 (permalink)  
Old June 23rd, 2012, 02:34 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

Do i have to still do

ptx.Variant.AddObject(newVariant);

if i still do

firstOne.Variants.Add(newVariant);
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #12 (permalink)  
Old June 23rd, 2012, 02:38 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

Take a look at this:

Code:
 
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
You're querying a package (firstOne) from ptx.Packages and then *add it again*. You shouldn't have to do that; you get a reference to the item and it's not removed from the Packages collection.

Also, you make this a bit shorter and more readable:

Code:
 
Package firstOne = ptx.Packages.First(o => o.packageID == packageID);
firstOne.Variants.Add(newVariant);
Quote:
Do i have to still do

ptx.Variant.AddObject(newVariant);

if i still do

firstOne.Variants.Add(newVariant);
No EF adds and tracks related entities to the object set for you automatically.

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!

Last edited by Imar; June 23rd, 2012 at 05:07 PM..
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Apocolypse2005 (June 23rd, 2012)
  #13 (permalink)  
Old June 23rd, 2012, 03:17 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

Brilliant, perfect.

Now I get one more error after all that.

Because my database looks like this

Packages (packageID, package_name , .... )
PackageVariant (packageID, variantID)
Variant (variantID, variant_name, .... )
ProductVariant (variantID, productID, quantity)
Product (productID, product_name, .... )

and I SaveChanges(); it says there is no Insert, Select, Delete functions created for, go define it, but I though thats what entity framework does for you!

Unable to update the EntitySet 'PackageVariant' because it has a DefiningQuery and no element exists in the element to support the current operation
__________________
Apocolypse2005, I'm a programmer - of sorts.

Last edited by Apocolypse2005; June 23rd, 2012 at 03:21 PM.. Reason: Add exception
Reply With Quote
  #14 (permalink)  
Old June 23rd, 2012, 03:36 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

EF only supports this transparant mapping (where the junction table is factored out of the model) if this table has no "payload"; in other words, only when it contains the two foreign keys will this work.

In your case, you'll need to instantiate an intermediate entity (called ProductVariant probably), associate it with a Product and a Variant and add it to the context.

For more info:

http://weblogs.asp.net/zeeshanhirani...framework.aspx
https://www.google.com/#q=EF+many+to...nship++payload

May I suggest you get a copy of Julie Lerman's book on the Entity Framework? Some of this can get pretty complex and here book is full of useful information.

Cheers,

Imae
__________________
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
  #15 (permalink)  
Old June 23rd, 2012, 03:52 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 have Murach's ado.net 3.5 Linq and the Entity Framework with C# 2008, will that be sufficient?
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #16 (permalink)  
Old June 23rd, 2012, 04:17 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

Any Chance you could reproduce my database design and walk me through being able to add in the entity?
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #17 (permalink)  
Old June 23rd, 2012, 05:06 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

If you create SQL CREATE TABLE statements to recreate the database structure as well as INSERT statements to create some default records, I'll take a look. Might take some time though....

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
  #18 (permalink)  
Old June 23rd, 2012, 05:13 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

This script was generated by SQL Management Studio 2008 R2

Code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Packages](
    [packageID] [uniqueidentifier] NOT NULL,
    [package_name] [nvarchar](150) NOT NULL,
    [package_desc] [nvarchar](1000) NULL,
 CONSTRAINT [PK_Packages] PRIMARY KEY CLUSTERED 
(
    [packageID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[Packages] ([packageID], [package_name], [package_desc]) VALUES (N'dca88e80-ec01-4267-b081-0f46219e1c0d', N'Wedding', N'For all the brides to be')
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Products](
    [productID] [uniqueidentifier] NOT NULL,
    [product_title] [nvarchar](150) NOT NULL,
    [product_desc] [nvarchar](1500) NULL,
    [product_price] [money] NULL,
    [product_time] [int] NULL,
    [product_discount_on] [int] NOT NULL,
    [product_discount_perc] [numeric](4, 2) NULL,
 CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
    [productID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[Products] ([productID], [product_title], [product_desc], [product_price], [product_time], [product_discount_on], [product_discount_perc]) VALUES (N'e0db1e20-41c7-4317-b455-237c67328260', N'Pedicure', N'Massaging of the feet include with each pedicure, so that you feel you have the ultimate experience', 50.0000, 120, 1, CAST(15.00 AS Numeric(4, 2)))
INSERT [dbo].[Products] ([productID], [product_title], [product_desc], [product_price], [product_time], [product_discount_on], [product_discount_perc]) VALUES (N'891d270e-9c00-450b-a471-4d6feccc6863', N'Hot stone massage', N'Hot stones ............', 40.0000, 60, 0, CAST(15.00 AS Numeric(4, 2)))
INSERT [dbo].[Products] ([productID], [product_title], [product_desc], [product_price], [product_time], [product_discount_on], [product_discount_perc]) VALUES (N'4f621e96-24f4-45b4-8053-888ceb0305b3', N'Mud Bath', N'This mud will make your skin absolutely beautiful and soft', 30.0000, 60, 0, CAST(0.00 AS Numeric(4, 2)))
INSERT [dbo].[Products] ([productID], [product_title], [product_desc], [product_price], [product_time], [product_discount_on], [product_discount_perc]) VALUES (N'4f93ff83-647d-466e-b55e-d7beedf22f03', N'Mud Bath', N'This mud will make your skin absolutely beautiful and soft', 30.0000, 60, 0, CAST(0.00 AS Numeric(4, 2)))
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Variants](
    [variantID] [uniqueidentifier] NOT NULL,
    [variant_name] [nvarchar](150) NULL,
    [variant_number_of_people] [int] NULL,
    [variant_discount_on] [int] NOT NULL,
    [variant_discount_perc] [numeric](4, 2) NULL,
    [variant_markip_rate] [numeric](4, 2) NOT NULL,
    [variant_total_cost] [money] NOT NULL,
 CONSTRAINT [PK_Variants] PRIMARY KEY CLUSTERED 
(
    [variantID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VariantProduct](
    [variantID] [uniqueidentifier] NOT NULL,
    [productID] [uniqueidentifier] NOT NULL,
    [quantity] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PackageVariant](
    [packageID] [uniqueidentifier] NOT NULL,
    [variantID] [uniqueidentifier] NOT NULL,
    [combined_desc] [nvarchar](max) NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PackageVariant]  WITH CHECK ADD  CONSTRAINT [FK_PackageVariant_Packages] FOREIGN KEY([packageID])
REFERENCES [dbo].[Packages] ([packageID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[PackageVariant] CHECK CONSTRAINT [FK_PackageVariant_Packages]
GO
ALTER TABLE [dbo].[PackageVariant]  WITH CHECK ADD  CONSTRAINT [FK_PackageVariant_Variants] FOREIGN KEY([variantID])
REFERENCES [dbo].[Variants] ([variantID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[PackageVariant] CHECK CONSTRAINT [FK_PackageVariant_Variants]
GO
ALTER TABLE [dbo].[VariantProduct]  WITH CHECK ADD  CONSTRAINT [FK_VariantProduct_Products] FOREIGN KEY([productID])
REFERENCES [dbo].[Products] ([productID])
GO
ALTER TABLE [dbo].[VariantProduct] CHECK CONSTRAINT [FK_VariantProduct_Products]
GO
ALTER TABLE [dbo].[VariantProduct]  WITH CHECK ADD  CONSTRAINT [FK_VariantProduct_Variants] FOREIGN KEY([variantID])
REFERENCES [dbo].[Variants] ([variantID])
GO
ALTER TABLE [dbo].[VariantProduct] CHECK CONSTRAINT [FK_VariantProduct_Variants]
GO
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #19 (permalink)  
Old June 23rd, 2012, 05:54 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

I see multiple tables have a Payload now. For which one are you trying to create an associated record? Are you trying to relate a Variant and a Product?
__________________
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
  #20 (permalink)  
Old June 23rd, 2012, 06:46 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

This is my post in stackoverflow, it explains what I'm trying to do. Relationships etc

http://stackoverflow.com/questions/1...tity-framework
__________________
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 02:11 PM.


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