MetaModel 类

定义

表示 ASP.NET 动态数据使用的一个或多个数据库。Represents one or multiple databases that are used by ASP.NET Dynamic Data.

public ref class MetaModel
public class MetaModel
type MetaModel = class
Public Class MetaModel
继承
MetaModel

示例

下面的示例演示如何使用 MetaModel type 执行以下任务,以便在 ASP.NET 网站中使用自动基架: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.

  • 评估路由路径 (确定指定表的 URL) 。Evaluate the routing path (determine the URL) for a specified table.

该示例包含一个页面及其代码隐藏文件。The example consists of a page and its code-behind file.

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

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

public class ProductMetaData
{
}
 
<%@ 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>

<%@ 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>

Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel

<MetadataType(GetType(ProductMetaData))> _
Partial Public Class Product

End Class


Public Class ProductMetaData
    
   

End Class

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;
    }
}
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Web.DynamicData

Partial Public Class PathModel
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        DynamicDataManager1.RegisterControl(GridDataSource1)
    End Sub

    ' Get the data model. 
    Public Function GetModel(ByVal defaultModel As Boolean) As MetaModel
        Dim model As MetaModel

        If defaultModel Then
            model = MetaModel.[Default]
        Else
            model = MetaModel.GetModel(GetType(AdventureWorksLTDataContext))
        End If
        Return model
    End Function

    ' Get the registered action path.
    Public Function EvaluateActionPath() As String
        Dim tableName As String = LinqDataSource1.TableName

        Dim model As MetaModel = GetModel(False)

        Dim actionPath As String = model.GetActionPath(tableName, System.Web.DynamicData.PageAction.List, GetDataItem())
        Return actionPath
    End Function
End Class

若要编译该示例,需要以下各项:To compile the example, you need the following:

请参阅此功能的运行时代码示例: 运行See a run-time code example of this feature: Run.

注解

MetaModel类型允许你为动态数据 Web 应用程序注册一个或多个数据上下文。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. 数据模型是一个对象,它将数据库的数据实体表示为 CLR 类型。A data model is an object that represents a database's data entities as CLR types. 动态数据支持基于 LINQ to SQL 的数据模型和 ADO.NET 实体框架上的数据模型。Dynamic Data supports data models based on LINQ to SQL and on the ADO.NET Entity Framework.

在 Visual Studio 中,可以使用 LINQ to SQL 类 模板或 ADO.NET 实体数据模型 模板生成数据模型类型。In Visual Studio, you can generate data-model types by using the LINQ to SQL Classes template or the ADO.NET Entity Data Model template. 这些模板将对象关系设计器 (O/R 设计器) 用于 LINQ to SQL 模型,或使用实体数据模型模型 (Entity Designer) 实体框架设计器。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.

构造函数

MetaModel()

实例化 MetaModel 类的新实例。Instantiates a new instance of the MetaModel class.

MetaModel(Boolean)

实例化 MetaModel 类的新实例。Instantiates a new instance of the MetaModel class.

属性

Default

获取应用程序创建的数据模型的第一个实例。Gets the first instance of a data model that is created by the application.

DynamicDataFolderVirtualPath

获取或设置 DynamicData 文件夹在网站中的虚拟路径。Gets or sets the virtual path of the DynamicData folder in the Web site.

EntityTemplateFactory

获取或设置与模型关联的 EntityTemplateFactory 对象。Gets or sets the EntityTemplateFactory object that is associated with the model.

FieldTemplateFactory

获取或设置自定义 IFieldTemplateFactory 接口。Gets or sets a custom IFieldTemplateFactory interface.

FilterFactory

获取或设置与模型关联的 FilterFactory 对象。Gets or sets the FilterFactory object that is associated with the model.

Tables

获取属于数据模型一部分的所有表的集合。Gets a collection of all the tables that are part of the data model.

VisibleTables

获取数据模型中可见表的集合。Gets a collection of the visible tables in the data model.

方法

CreateTable(TableProvider)

实例化 MetaTable 对象。Instantiates a MetaTable object.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetActionPath(String, String, Object)

返回与特定表关联的操作路径。Returns the action path that is associated with a specific table.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetModel(Type)

返回指定上下文的数据模型实例。Returns the data-model instance for the specified context.

GetTable(String)

返回与指定表关联的元数据。Returns the metadata that is associated with the specified table.

GetTable(String, Type)

返回描述指定表的元数据。Returns the metadata that describes the specified table.

GetTable(Type)

返回描述指定表的元数据。Returns the metadata that describes the specified table.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RegisterContext(DataModelProvider)

通过使用数据模型提供程序来注册数据上下文实例。Registers a data context instance by using a data-model provider.

RegisterContext(DataModelProvider, ContextConfiguration)

通过使用指定的上下文配置并启用数据模型提供程序,注册数据上下文实例。Registers a data-context instance by using the specified context configuration and by enabling a data-model provider.

RegisterContext(Func<Object>)

注册上下文工厂指定的数据上下文。Registers the data context that is specified by a context factory.

RegisterContext(Func<Object>, ContextConfiguration)

通过使用指定的上下文配置并启用自定义构造函数,注册数据上下文实例。Registers a data-context instance by using the specified context configuration and by enabling a custom constructor.

RegisterContext(Type)

注册数据上下文实例。Registers a data-context instance.

RegisterContext(Type, ContextConfiguration)

使用指定的上下文配置注册数据上下文实例。Registers a data-context instance by using the specified context configuration.

ResetRegistrationException()

重置可能发生的任何前一个上下文注册错误。Resets any previous context registration error that might have occurred.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
TryGetTable(String, MetaTable)

尝试获取与指定表关联的元数据。Attempts to get the metadata that is associated with the specified table.

TryGetTable(Type, MetaTable)

尝试获取与指定表关联的元数据。Attempts to get the metadata that is associated with the specified table.

适用于