1 out of 1 rated this helpful - Rate this topic

MetaModel Class

Represents one or multiple databases that are used by ASP.NET Dynamic Data.

System.Object
  System.Web.DynamicData.MetaModel

Namespace:  System.Web.DynamicData
Assembly:  System.Web.DynamicData (in System.Web.DynamicData.dll)
public class MetaModel

The MetaModel type exposes the following members.

  Name Description
Public method MetaModel() Instantiates a new instance of the MetaModel class.
Public method MetaModel(Boolean) Instantiates a new instance of the MetaModel class.
Top
  Name Description
Public property Static member Default Gets the first instance of a data model that is created by the application.
Public property DynamicDataFolderVirtualPath Gets or sets the virtual path of the DynamicData folder in the Web site.
Public property EntityTemplateFactory Gets or sets the EntityTemplateFactory object that is associated with the model.
Public property FieldTemplateFactory Gets or sets a custom IFieldTemplateFactory interface.
Public property FilterFactory Gets or sets the FilterFactory object that is associated with the model.
Public property Tables Gets a collection of all the tables that are part of the data model.
Public property VisibleTables Gets a collection of the visible tables in the data model.
Top
  Name Description
Protected method CreateTable Instantiates a MetaTable object.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetActionPath Returns the action path that is associated with a specific table.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Static member GetModel Returns the data-model instance for the specified context.
Public method GetTable(String) Returns the metadata that is associated with the specified table.
Public method GetTable(Type) Returns the metadata that describes the specified table.
Public method GetTable(String, Type) Returns the metadata that describes the specified table.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method RegisterContext(DataModelProvider) Registers a data context instance by using a data-model provider
Public method RegisterContext(Func<Object>) Registers the data context that is specified by a context factory.
Public method RegisterContext(Type) Registers a data-context instance.
Public method RegisterContext(DataModelProvider, ContextConfiguration) Registers a data-context instance by using the specified context configuration and by enabling a data-model provider.
Public method RegisterContext(Func<Object>, ContextConfiguration) Registers a data-context instance by using the specified context configuration and by enabling a custom constructor.
Public method RegisterContext(Type, ContextConfiguration) Registers a data-context instance by using the specified context configuration.
Public method Static member ResetRegistrationException Resets any previous context registration error that might have occurred.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryGetTable(String, MetaTable) Attempts to get the metadata that is associated with the specified table.
Public method TryGetTable(Type, MetaTable) Attempts to get the metadata that is associated with the specified table.
Top
Exception Condition
InvalidOperationException

Can be thrown by any method if there has been a data context registration error.

The MetaModel type lets you register one or multiple data contexts for a Dynamic Data Web application.

A data context is an object that represents a database connection. A data context has access to one data model which represents a database that is available through that connection. A data model is an object that represents a database's data entities as CLR types. Dynamic Data supports data models based on LINQ to SQL and on the ADO.NET Entity Framework.

In Visual Studio 2010, you can generate data-model types by using the LINQ to SQL Classes template or the ADO.NET Entity Data Model template. These templates use the Object Relational Designer (O/R Designer) for the LINQ to SQL model, or the ADO.NET Entity Data Model Designer (Entity Designer) for the Entity Framework model.

The following example shows how to use MetaModel type to perform the following tasks in order to use automatic scaffolding in an ASP.NET Web site:

  • Get the data model for the default data context.

  • Get the data model for a specified data context.

  • Evaluate the routing path (determine the URL) for a specified table.

The example consists of a page and its code-behind file.


<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="PathModel.aspx.vb" 
Inherits="PathModel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Path Model</title>
</head>
<body>
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"
        AutoLoadForeignKeys="true" />

    <h3>GetActionPath</h3>

    <form id="form1" runat="server">
        <asp:GridView ID="GridDataSource1" runat="server"
        AutoGenerateColumns="false"
        DataSourceID="LinqDataSource1"
        AllowPaging="true">
        <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" 
            NavigateUrl="<%#EvaluateActionPath()%>">ListDetails</asp:HyperLink>
          </ItemTemplate>
         </asp:TemplateField>
          <asp:DynamicField DataField="FirstName" />
          <asp:DynamicField DataField="LastName" />
        </Columns>
      </asp:GridView>

        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        TableName="Customers"
        ContextTypeName="AdventureWorksLTDataContext" >
      </asp:LinqDataSource>
    </form>
</body>

</html>




<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="PathModel.aspx.cs" 
Inherits="PathModel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Path Model</title>
</head>
<body>
    <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"
        AutoLoadForeignKeys="true" />

    <h3>GetActionPath</h3>

    <form id="form1" runat="server">
        <asp:GridView ID="GridDataSource1" runat="server"
        AutoGenerateColumns="false"
        DataSourceID="LinqDataSource1"
        AllowPaging="true">
        <Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:HyperLink ID="HyperLink1" runat="server" 
            NavigateUrl="<%#EvaluateActionPath()%>">ListDetails</asp:HyperLink>
          </ItemTemplate>
         </asp:TemplateField>
          <asp:DynamicField DataField="FirstName" />
          <asp:DynamicField DataField="LastName" />
        </Columns>
      </asp:GridView>

        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        TableName="Customers"
        ContextTypeName="AdventureWorksLTDataContext" >
      </asp:LinqDataSource>
    </form>
</body>

</html>




using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

[MetadataType(typeof(ProductMetaData))]
public partial class Product
{

}


public class ProductMetaData
{



}
 



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.DynamicData;

public partial class PathModel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DynamicDataManager1.RegisterControl(GridDataSource1);
    }

    // Get the data model. 
    public MetaModel GetModel(bool defaultModel)
    {
        MetaModel model;

        if (defaultModel)
            model = MetaModel.Default;
        else
            model =
               MetaModel.GetModel(typeof(AdventureWorksLTDataContext));
        return model;
    }

    // Get the registered action path.
    public string EvaluateActionPath()
    {
        string tableName = LinqDataSource1.TableName;

        MetaModel model = GetModel(false);

        string actionPath =
            model.GetActionPath(tableName,
                System.Web.DynamicData.PageAction.List, GetDataItem());
        return actionPath;
    }
}


To compile the example, you need the following:

See a run-time code example of this feature: Run.

.NET Framework

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ