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 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 January 25th, 2011, 01:26 AM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 Linq and ado.net entity framework

Page 510, chapter 14, after adding the following code in the event handler, following error is thrown.

'PlanetWroxModel.Picture' does not contain a definition for 'PhotoAlbumId' and no extension method 'PhotoAlbumId' accepting a first argument of type 'PlanetWroxModel.Picture' could be found (are you missing a using directive or an assembly reference?)


My ManagePhotoAlbum.aspx.cs is as follows.

Code:
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlanetWroxModel;


public partial class _ManagePhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void EntityDataSource1_Inserting(object sender, EntityDataSourceChangingEventArgs e)
    {
                
            int photoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
            Picture myPicture = (Picture)e.Entity;
            myPicture.PhotoAlbumId = photoAlbumId;
        
    }
}
kindly Help..!!!
Thanks in advance...
Reply With Quote
  #2 (permalink)  
Old January 25th, 2011, 03:41 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

When you created the model in Step 4 on page 499, did you leave the option "Include foreign key columns in the model" checked? You need that option in order for the AlbumId to show up.

Also, check that you created the correct relationship between the tables as explained in the same exercise before you add the tables to the model.

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
  #3 (permalink)  
Old January 25th, 2011, 06:03 AM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I checked out the same, but "Include Foreign key Columns in the model" check box is not reflecting in the "Entity Data Model Wizard". I'm using VWD xpress edition 2008. How to enable/display the same..?


rgds,

pandukal21
Reply With Quote
  #4 (permalink)  
Old January 25th, 2011, 06:06 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You need VWD or VS 2010 to follow along with this book. The foreign key properties were introduced in EF 4 which ships with .NET 4 / VS 2010, and not with .NET 3.5 / VS 2008.

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:
pandukal21 (January 25th, 2011)
  #5 (permalink)  
Old January 25th, 2011, 06:25 AM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Managed to reach till chapter 14 with VWD 2008. Anyways, thank you so much for your information..

Rgds,

pandukal21
Reply With Quote
  #6 (permalink)  
Old January 26th, 2011, 06:01 PM
Registered User
 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Facing The Same problem

Kindly,the same problem faced me when I tried to write that code in the code behind page "ManagePhotoAlbum.aspx" when I tried to write the statement
"mypicture.PhotoAlbumId=photoAlbumId"
but VWD didn't recognize that forign key PhotoAlbumId
really I'm using VWD Express 2008 with .Net Framework 3.5 .
In that case Mr/Imar What the code could be if I using VWD2008/EF3.5 not VWD2010/EF4 as in the book to recover that problem.
The remainder of the book is fine with me using that version of VWD
Waiting your reply.......
Thanks in Advance
Reply With Quote
  #7 (permalink)  
Old January 26th, 2011, 06:56 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're probably better off downloading the free Express Edition of VWD 2010. You'll run into other problems later as well

Anyway, since there's no PhotoAlbumId, you need to query an actual instance. E.g. something like:

Quote:
using (PlanetWroxContext context = new PlanetWroxContext())
{
...
myPicture.PhotoAlbum = context.PhotoAlbums.Where(a => a.Id == photoAlbumId).First();
}
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
The Following User Says Thank You to Imar For This Useful Post:
mohamedz (January 27th, 2011)
  #8 (permalink)  
Old January 27th, 2011, 10:47 AM
Registered User
 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default RunTime Error

--I had written the code in the page "ManagePhotoAlbum.aspx" as follows:
Imports System.Linq
Imports PlanetWroxModel
Partial Class _ManagePhotoAlbum
Inherits BasePage

Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedE ventArgs) Handles EntityDataSource1.Inserted
Dim a As New PhotoAlbum
Dim Context As New PlanetWroxEntities1
Dim photoAlbumId As Integer = _
Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"))
Dim myPicture As Picture = CType(e.Entity, Picture)
myPicture.PhotoAlbum = Context.PhotoAlbum.Where(a.Id = photoAlbumId).First()
End Sub
End Class


and when I requested the page in the browser and entered the required Description,ToolTip, and ImageURL for the picture and clicked Insert,I got the following RunTime Error:-
Object Reference not set to an instance of an Object
for the line
myPicture.PhotoAlbum = Context.PhotoAlbum.Where(a.Id = photoAlbumId).First()
What the error can be

--In your code in the last reply,you wrote a line in the code:-
myPicture.PhotoAlbum = context.PhotoAlbums.Where(a => a.Id == photoAlbumId).First();

Kindly,What a refers to?

Waiting your Reply......

Thanks in Advance
Reply With Quote
  #9 (permalink)  
Old January 28th, 2011, 05:19 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
What a refers to?
This is a range variable. See page 488 for more details.

Sorry, the example I gave was in C#. I also found some other issues that needed to be fixed. In VB you can do something like this:

Code:
Using myContext As New PlanetWroxEntities()
  Dim photoAlbum As PhotoAlbum = myContext.PhotoAlbums.Where(Function(album) album.Id = photoAlbumId).First()
  myContext.Detach(photoAlbum)
  e.Context.AttachTo("PhotoAlbums", photoAlbum)
  myPicture.PhotoAlbum = photoAlbum
End Using
This queries the album from the new context, detaches it and then reattaches it to the existing context.

You can also avoid calling the database by creating a fake PhotoAlbum and attaching that:

Code:
Dim photoAlbum As PhotoAlbum = New PhotoAlbum() With {.Id = photoAlbumId}
e.Context.AttachTo("PhotoAlbums", photoAlbum)
myPicture.PhotoAlbum = photoAlbum
BTW, you are calling this code in the Inserted event, while you should be using the Inserting event.

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; January 29th, 2011 at 01:15 PM.. Reason: Removed Using block from the second sample
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
question for ADO.NET chapter 14 stram BOOK: Beginning ASP.NET 4 : in C# and VB 6 September 27th, 2010 06:44 AM
Linq to SQL and the Entity Framework marko_ln BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 4 February 25th, 2010 11:20 AM
confusing in ADO.NET Entity Data Model,Linq to sql angelboy ADO.NET 5 November 21st, 2008 05:01 PM
Should I learn ado.net Or linq? chobo2 LINQ 2 January 4th, 2008 06:59 PM



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


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