locked
Dynamic Data How-to: no accessible tables? RRS feed

  • Question

  • User-718146471 posted

    Ok, I'm trying to learn something new and I've been following along this how-to video on dynamic data.  Everything was going along fine and I do my debugging as shown in the video.  The author's code worked but mine does not.  I have mirrored everything step by step as shown.  The error I get is this:

    There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages. 

    Global.asax

    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.ComponentModel.DataAnnotations" %>
    <%@ Import Namespace="System.Web.Routing" %>
    <%@ Import Namespace="System.Web.DynamicData" %>
    <%@ Import Namespace="System.Web.UI" %>
    
    <script RunAt="server">
        private static MetaModel s_defaultModel = new MetaModel();
        public static MetaModel DefaultModel {
            get {
                return s_defaultModel;
            }
        }
    
        public static void RegisterRoutes(RouteCollection routes) {
            MetaModel model = new MetaModel();
    
            model.RegisterContext(typeof(NorthwindDataContext),
                new ContextConfiguration() { ScaffoldAllTables = true });
    
            // The following statement supports separate-page mode, where the List, Detail, Insert, and 
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });
    
            // The following statements support combined-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using the same page. To enable this mode, uncomment the
            // following routes and comment out the route definition in the separate-page mode section above.
            //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
            //    Action = PageAction.List,
            //    ViewName = "ListDetails",
            //    Model = DefaultModel
            //});
    
            //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
            //    Action = PageAction.Details,
            //    ViewName = "ListDetails",
            //    Model = DefaultModel
            //});
        }
    
        private static void RegisterScripts() {
            ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
            {
                Path = "~/Scripts/jquery-1.7.1.min.js",
                DebugPath = "~/Scripts/jquery-1.7.1.js",
                CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js",
                CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js",
                CdnSupportsSecureConnection = true,
                LoadSuccessExpression = "window.jQuery"
            });
        }
        
        void Application_Start(object sender, EventArgs e) {
            RegisterRoutes(RouteTable.Routes);
            RegisterScripts();
        }
    
    </script>
    

    How To Video: http://www.asp.net/web-forms/videos/aspnet-dynamic-data/your-first-scaffold-and-what-is-dynamic-data

    Thursday, January 9, 2014 10:25 AM

Answers

  • User-330204900 posted

    OK a sinple issue see your code here

        public static void RegisterRoutes(RouteCollection routes) {
            MetaModel model = new MetaModel();
    
            model.RegisterContext(typeof(NorthwindDataContext),
                new ContextConfiguration() { ScaffoldAllTables = true });
    
            // The following statement supports separate-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

    you are iinitialising model when you shoudl be initialising DefaultModel.

    Just strip out this code

    MetaModel model = new MetaModel();

    and use the DefaultModel :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 4:42 PM
  • User-330204900 posted

    That project template should be a simple as adding an entity Framwork Data model editing the Global.asax.cs witht he model name and setting ScffoldColumns to true and it should just runf then I have don it literally hundreds of times.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 7:16 PM

All replies

  • User-330204900 posted

    OK a sinple issue see your code here

        public static void RegisterRoutes(RouteCollection routes) {
            MetaModel model = new MetaModel();
    
            model.RegisterContext(typeof(NorthwindDataContext),
                new ContextConfiguration() { ScaffoldAllTables = true });
    
            // The following statement supports separate-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

    you are iinitialising model when you shoudl be initialising DefaultModel.

    Just strip out this code

    MetaModel model = new MetaModel();

    and use the DefaultModel :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 4:42 PM
  • User-330204900 posted

    remember the videos are OLD .Net 3.51 the principals are the same but the plumbing has changed :)

    the old project template declared the Metamodel like this:

    MetaModel model = new MetaModel();

    and now it is declared like this:

    private static MetaModel s_defaultModel = new MetaModel();
    public static MetaModel DefaultModel
    {
        get
        {
            return s_defaultModel;
        }
    }

    it's always worth comparing what the video shows with a vanila Project created with the latest version of Visual Studio

    Thursday, January 9, 2014 4:54 PM
  • User-718146471 posted

    I'll give this a try and get back to you, thanks!

    Friday, January 10, 2014 7:09 AM
  • User-718146471 posted

    Ok, I made the changes you recommended but now I'm getting a different error entirely:

    Compiler Error Message: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data' 
    (are you missing an assembly reference?)
    
    Source Error:
    
    
     
    
    Line 12: namespace DynamicDataSite.App_Code
    Line 13: {
    Line 14: 	using System.Data.Linq;
    Line 15: 	using System.Data.Linq.Mapping;
    Line 16: 	using System.Data;
      
    
    Source File: d:\Projects\DynamicDataSite\DynamicDataSite\App_Code\Northwind.designer.cs    Line: 14 

    Friday, January 10, 2014 7:52 AM
  • User-718146471 posted

    I think this error is fooey because VS2012 built the Northwind.designer.cs file:

     

    #pragma warning disable 1591
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.18408
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace DynamicDataSite.App_Code
    {
    	using System.Data.Linq;
    	using System.Data.Linq.Mapping;
    	using System.Data;
    	using System.Collections.Generic;
    	using System.Reflection;
    	using System.Linq;
    	using System.Linq.Expressions;
    	using System.ComponentModel;
    	using System;
    	
    	
    

    As you can see, it is referenced in the file and it doesn't have the red squiggly nonsense.

    Friday, January 10, 2014 7:53 AM
  • User-718146471 posted

    This is down right irritating. I fix one problem and gain another.

    System.ArgumentException was unhandled by user code HResult=-2147024809 Message=The context type 'DynamicDataSite._Default' is not supported.

    Source=System.Web.DynamicData StackTrace: at System.Web.DynamicData.MetaModel.RegisterContext(Func`1 contextFactory, ContextConfiguration configuration) at System.Web.DynamicData.MetaModel.RegisterContext(Type contextType, ContextConfiguration configuration) at DynamicDataSite.Global.RegisterRoutes(RouteCollection routes) in d:\Projects\DynamicDataSite\DynamicDataSite\Global.asax.cs:line 38 at DynamicDataSite.Global.Application_Start(Object sender, EventArgs e) in d:\Projects\DynamicDataSite\DynamicDataSite\Global.asax.cs:line 80 InnerException:

    Global.asax

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Web;
    using System.Web.DynamicData;
    using System.Web.Routing;
    using System.Web.UI;
    
    namespace DynamicDataSite
    {
        public class Global : System.Web.HttpApplication
        {
            private static MetaModel s_defaultModel = new MetaModel();
            public static MetaModel DefaultModel
            {
                get
                {
                    return s_defaultModel;
                }
            }
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                //                    IMPORTANT: DATA MODEL REGISTRATION 
                // Uncomment this line to register an ADO.NET Entity Framework model for ASP.NET Dynamic Data.
                // Set ScaffoldAllTables = true only if you are sure that you want all tables in the
                // data model to support a scaffold (i.e. templates) view. To control scaffolding for
                // individual tables, create a partial class for the table and apply the
                // [ScaffoldTable(true)] attribute to the partial class.
                // Note: Make sure that you change "YourDataContextType" to the name of the data context
                // class in your application.
                // See http://go.microsoft.com/fwlink/?LinkId=257395 for more information on how to register Entity Data Model with Dynamic Data            
                //DefaultModel.RegisterContext(() =>
                //{
                //    return ((IObjectContextAdapter)new YourDataContextType()).ObjectContext;
                //}, new ContextConfiguration() { ScaffoldAllTables = false });
    
                // The following registration should be used if YourDataContextType does not derive from DbContext
                DefaultModel.RegisterContext(typeof(_Default), new ContextConfiguration() { ScaffoldAllTables = true });
    
                // The following statement supports separate-page mode, where the List, Detail, Insert, and 
                // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
                // route definition, and comment out the route definitions in the combined-page mode section that follows.
                routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
                {
                    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                    Model = DefaultModel
                });
    
                // The following statements support combined-page mode, where the List, Detail, Insert, and
                // Update tasks are performed by using the same page. To enable this mode, uncomment the
                // following routes and comment out the route definition in the separate-page mode section above.
                //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
                //    Action = PageAction.List,
                //    ViewName = "ListDetails",
                //    Model = DefaultModel
                //});
    
                //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
                //    Action = PageAction.Details,
                //    ViewName = "ListDetails",
                //    Model = DefaultModel
                //});
            }
    
            private static void RegisterScripts()
            {
                ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
                {
                    Path = "~/Scripts/jquery-1.7.1.min.js",
                    DebugPath = "~/Scripts/jquery-1.7.1.js",
                    CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js",
                    CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.js",
                    CdnSupportsSecureConnection = true,                
                    LoadSuccessExpression = "window.jQuery"
                });
            }
    
            void Application_Start(object sender, EventArgs e)
            {
                RegisterRoutes(RouteTable.Routes);
                RegisterScripts();
            }
    
        }
    }
    

    Friday, January 10, 2014 8:05 AM
  • User-718146471 posted

    Easiest way to fix this was to use asp.net 3.5. Now the sample seems to work better.

    Friday, January 10, 2014 8:11 AM
  • User-718146471 posted

    Arghhh, one step forward, two back. This crap is annoying. I click on the Table Name link and get an error on page http://localhost:64152/DynamicDataWebsite/Orders/List.aspx

     

    Server Error in '/DynamicDataWebsite' Application.
    --------------------------------------------------------------------------------
    
    
    Unable to cast object of type 'DynamicDataWebsite.NorthwindDataContext' to type 'System.Data.Objects.ObjectContext'. 
    
    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.InvalidCastException: Unable to cast object of type 'DynamicDataWebsite.NorthwindDataContext' to type 'System.Data.Objects.ObjectContext'. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified
    using the exception stack trace below. Stack Trace: [InvalidCastException: Unable to cast object of type 'DynamicDataWebsite.NorthwindDataContext' to type 'System.Data.Objects.ObjectContext'.] System.Web.UI.WebControls.EntityDataSourceView.ConstructContext() +690 System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments, Creator qbConstructor) +164 System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +192 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.GridView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72 System.Web.UI.Control.EnsureChildControls() +87 System.Web.UI.Control.PreRenderRecursiveInternal() +44 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.5472; ASP.NET Version:2.0.50727.5459

    Friday, January 10, 2014 8:16 AM
  • User-330204900 posted

    Are you using the Dynamic Data Project Template to test this?

    Friday, January 10, 2014 11:08 AM
  • User-718146471 posted

    Yes I am.

    Friday, January 10, 2014 11:51 AM
  • User-330204900 posted

    OK What version of Visual Studio?

    Friday, January 10, 2014 12:17 PM
  • User-718146471 posted

    Visual Studio 2012 Professional

    Friday, January 10, 2014 12:27 PM
  • User-330204900 posted

    That project template should be a simple as adding an entity Framwork Data model editing the Global.asax.cs witht he model name and setting ScffoldColumns to true and it should just runf then I have don it literally hundreds of times.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 7:16 PM
  • User-330204900 posted

    e-mail me direct and we'll walk through, if your in the UK we coudl skype :)

    Friday, January 10, 2014 7:17 PM
  • User-718146471 posted

    I'll try it again going at it just with the template and no modifications.

    Monday, January 13, 2014 3:15 PM
  • User-330204900 posted

    great let me know how you get on maybe I need to think of doing some startup videos for DD :)

    Monday, January 13, 2014 3:25 PM
  • User-718146471 posted

    SJ, I'm good man. Redid the project from scratch, made the one little change to the web.config and all is working good. :)

    Wednesday, January 15, 2014 9:02 AM
  • User-330204900 posted

    What did you need to chang ein the web.config?

    Wednesday, January 15, 2014 9:06 AM
  • User-718146471 posted

    Scaffold all tables = true like you told me to :)

    Wednesday, January 15, 2014 9:36 AM
  • User-718146471 posted

    OK a sinple issue see your code here

    public static void RegisterRoutes(RouteCollection routes) {
            MetaModel model = new MetaModel();
    
            model.RegisterContext(typeof(NorthwindDataContext),
                new ContextConfiguration() { ScaffoldAllTables = true });
    
            // The following statement supports separate-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

    you are iinitialising model when you shoudl be initialising DefaultModel.

    Just strip out this code

    MetaModel model = new MetaModel();

    and use the DefaultModel :)

    This is what did it for me after I re-created the project from scratch.

    Wednesday, January 15, 2014 9:37 AM
  • User-330204900 posted

    OK it was the Global.asax.cs you made a change to :) you said you had to make a change to the web.config that was what threw me :) clad you are working. Any questions and I 'm here.

    Wednesday, January 15, 2014 10:00 AM