Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB 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 September 16th, 2013, 04:48 PM
Registered User
 
Join Date: Sep 2013
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 14 - All.aspx error

Hi Imar

Thank you for this informative book. I need your help on Chapter 14. When I try to open All.aspx I get the following error:

Server Error in '/xxxxxxxxx' Application.
The type 'Review' is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute. POCO entities that do not use EdmEntityTypeAttribute cannot be contained in the same assembly as non-POCO entities that use EdmEntityTypeAttribute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The type 'Review' is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute. POCO entities that do not use EdmEntityTypeAttribute cannot be contained in the same assembly as non-POCO entities that use EdmEntityTypeAttribute.

Source Error:
Line 7: Using myEntities As New PlanetWroxEntities()
Line 8:
Line 9: Dim authorizedReviews = From review In myEntities.Reviews
Line 10: Where review.Authorized = True
Line 11: Order By review.CreateDateTime Descending

Source File: C:\inetpub\wwwroot\xxxxxxxxx\All.aspx.vb Line: 9

Stack Trace:
[InvalidOperationException: The type 'Review' is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute. POCO entities that do not use EdmEntityTypeAttribute cannot be contained in the same assembly as non-POCO entities that use EdmEntityTypeAttribute.]
System.Data.Entity.Internal.InternalContext.Update EntitySetMappingsForType(Type entityType) +148
System.Data.Entity.Internal.InternalContext.GetEnt itySetAndBaseTypeForType(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Ini tialize() +52
System.Data.Entity.Internal.Linq.InternalSet`1.get _InternalContext() +15
System.Data.Entity.Infrastructure.DbQuery`1.System .Linq.IQueryable.get_Provider() +37
System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +63
Reviews_All.Page_Load(Object sender, EventArgs e) in C:\inetpub\wwwroot\xxxxxxxxx\Reviews\All.aspx.vb:9
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044

Here is the source code:

All.aspx


Code:
 
<%@ Page Title="All Reviews" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="All.aspx.vb" Inherits="Reviews_All" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
  <asp:GridView ID="GridView1" runat="server">
  </asp:GridView>
</asp:Content>
All.aspx.vb

Code:
Imports PlanetWroxModel

Partial Class Reviews_All
  Inherits BasePage

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Using myEntities As New PlanetWroxEntities()

            Dim authorizedReviews = From review In myEntities.Reviews
                              Where review.Authorized = True
                              Order By review.CreateDateTime Descending
                              Select review
            GridView1.DataSource = authorizedReviews
            GridView1.DataBind()
        End Using

  End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
End Class
Please help me solve this POCO Problem.

Thank you
 
Old September 17th, 2013, 01:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Did you set the Code Generation Strategy to None for the EF diagram and delete the .tt files? Steps 7 and futher on Page 502 have more detail.

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!
The Following User Says Thank You to Imar For This Useful Post:
Dbug (September 17th, 2013)
 
Old September 17th, 2013, 08:35 PM
Registered User
 
Join Date: Sep 2013
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you very much Imar. I set the Code Generation Strategy to None and followed the steps as you recommended and that solved the problem.

I really appreciate your help.

Thanks again.
 
Old September 18th, 2013, 04:05 AM
Registered User
 
Join Date: Sep 2013
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Imar

I just wanted to point out that after setting the Code Generation Strategy to None, the All.aspx and the AllByGenre.aspx files are displaying all the info with no problems, but setting the Code Generation Strategy to None has triggered a new problem for me.

For the Namespace PlanetWroxModel, I am now getting the following warning:

Namespace or type specified in the Imports 'PlanetWroxModel' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

With the above mentioned warning active, the files All.aspx and the AllByGenre.aspx are working without any problems, but when I get to try work on the NewPhotoAlbum.aspx and ManagePhotoAlbum.aspx I am now getting an error because the Namespace is out of scope.

Error for ManagePhotoAlbum.aspx:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'PlanetWroxModel.Picture' is not defined.

Source Error:
Line 7: <li style="">Description:
Line 8: <asp:RequiredFieldValidator ID="reqDesc" ControlToValidate="DescriptionTextBox" runat="server" ErrorMessage="Enter a description." />
Line 9: <asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Text='<%# BindItem.Description %>' />
Line 10: <br />
Line 11: ToolTip:
Source File: C:\inetpub\wwwroot\xxxxxxx \ManagePhotoAlbum.aspx Line: 9

Error for NewPhotoAlbum.aspx:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'PhotoAlbum' is not defined.

Source Error:
Line 6: Protected Sub EntityDataSource1_Inserted(sender As Object, e As EntityDataSourceChangedEventArgs) Handles EntityDataSource1.Inserted
Line 7: If (e.Entity IsNot Nothing) Then
Line 8: Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
Line 9: Response.Redirect(String.Format("ManagePhotoAlbum. aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
Line 10: End If
Source File: C:\inetpub\wwwroot\xxxxxxx \NewPhotoAlbum.aspx.vb Line: 8


Code Behind - ManagePhotoAlbum.aspx.vb
Code:
Imports PlanetWroxModel

Partial Class _ManagePhotoAlbum
  Inherits BasePage

  Protected Sub EntityDataSource1_Inserting(sender As Object, e As EntityDataSourceChangingEventArgs) Handles EntityDataSource1.Inserting
    Dim photoAlbumId As Integer = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"))
    Dim myPicture As Picture = CType(e.Entity, Picture)
    myPicture.PhotoAlbumId = photoAlbumId

    Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload1"), FileUpload)
    Dim virtualFolder As String = "~/GigPics/"
    Dim physicalFolder As String = Server.MapPath(virtualFolder)
    Dim fileName As String = Guid.NewGuid().ToString()
    Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
    FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension))
    myPicture.ImageUrl = virtualFolder + fileName + extension
  End Sub

  Protected Sub ListView1_ItemInserting(sender As Object, e As ListViewInsertEventArgs) Handles ListView1.ItemInserting
    Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload1"), FileUpload)
    If Not FileUpload1.HasFile OrElse Not FileUpload1.FileName.ToLower().EndsWith(".jpg") Then
      Dim cusValImage As CustomValidator = CType(ListView1.InsertItem.FindControl("cusValImage"), CustomValidator)
      cusValImage.IsValid = False
      e.Cancel = True
    End If
  End Sub
End Class

Code Behind - NewPhotoAlbum.aspx.vb
Code:
Imports PlanetWroxModel

Partial Class _NewPhotoAlbum
  Inherits BasePage

  Protected Sub EntityDataSource1_Inserted(sender As Object, e As EntityDataSourceChangedEventArgs) Handles EntityDataSource1.Inserted
    If (e.Entity IsNot Nothing) Then
            Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
      Response.Redirect(String.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}", myPhotoAlbum.Id.ToString()))
    End If
  End Sub
End Class
Somehow when I set the Code Generation Strategy to Default, the NewPhotoAlbum.aspx and ManagePhotoAlbum.aspx files are working without a problem and I can even insert new albums to the database and view them.

Hence the All.aspx and the AllByGenre.aspx files are not working with this setting.

I deleted the .edmx files and all related files and did the exercise again to try and see where I went wrong but the results are the same when the Code Generation Strategy is set to Default some files work and the Namespace is in scope and visa-versa.

Could you please help.

Thanks
 
Old September 18th, 2013, 04:21 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at the code behind of the EDMX file in App_Code (it's called PlanetWrox.Designer.vb). This is the code that gets generated by Visual Studio. Do you see something like Namespace PlanetWroxModel in there that in turn defines the types such as PlanetWroxEntities and Picture? Does any of these types have a "1" suffix in their name?

It looks like the generated code is different than it should be. This could be the case when you've used different names for the model, or if the old code file still existed. Try redoing the exercise with a clean setup, like this:

1. Remove the EDMX from within Visual Studio
2. Check on disk (not in VS) that there are no files in App_Code related to the EDMX file.
3. From web.config, remove the connection string that EF put there.

Then recreate the model, set the Code Generation Strategy to Default (not to None as you currently have), delete the .tt files from App_Code and rebuild the web site (menu Build | Rebuild).

This works for me, so hopefully it works for you as well. If not, maybe you can send me your project and I'll have a look.

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!
The Following User Says Thank You to Imar For This Useful Post:
Dbug (September 18th, 2013)
 
Old September 18th, 2013, 06:15 AM
Registered User
 
Join Date: Sep 2013
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks again Imar

I recreated the model following your instructions above and all the pages are now loading properly and I also rebuilt the website and the validation completed successfully.

Thank you very much and have a nice day.
 
Old September 18th, 2013, 09:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Glad to hear it's working and thanks for the follow up.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 - Error inserting usmaak BOOK: Beginning ASP.NET 4.5 : in C# and VB 12 March 10th, 2013 11:15 PM
Chapter 14 Code Error? pherank BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 4th, 2008 09:05 PM
Wrox United - chapter 14 - error SqlDependency tom_10000001 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 November 10th, 2006 06:22 AM





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