Databases Reference
In-Depth Information
Data Access Support
In addition to the metadata information, the
MetaTable
class also plays an important role
in implementing data access logic in Dynamic Data web applications:
public Type DataContextType { get; }
public string DataContextPropertyName { get; }
The
DataContextType
property of the
MetaTable
class returns a
Type
object that represents
the data context class generated by the data model. This class is typically derived from the
ObjectContext
base class in Entity Framework and the
DataContext
class in LINQ to SQL.
The
DataContextPropertyName
property returns the name of the property you would use
to query the table rows, such as the Products property in the NorthwindEntities object
context. At run time, this information is used by the
DynamicDataManager
control in page
templates to configure the
EntityDataSource
and
LinqDataSource
controls responsible for
retrieving entity data and saving changes to the database.
NOTE
It is significant that
DataContextType
is a property of the
MetaTable
class and not
the
MetaModel
. A single
MetaModel
instance can have tables from different data
contexts registered by separate calls to the
RegisterContext
method. If different
contexts have the tables with the same name, your code needs to distinguish them by
passing the context type to the
GetTable
method of the
MetaModel
class, along with
the table name.
As you might recall from our discussion of the
RegisterContext
method of the
MetaModel
class, if the object context requires additional initialization tasks, you can provide a
factory method when registering it, instead of simply giving it the type:
public virtual object CreateContext();
The
CreateContext
method of the
MetaTable
class is responsible for creating the context
object, either by creating an instance of the registered type or by calling the context
factory method, if that's how the context was registered with the
MetaModel
:
public IQueryable GetQuery();
public virtual IQueryable GetQuery(object context);
The
MetaTable
class also provides two overloaded
GetQuery
methods that can be used to
query any table dynamically. This is how the ForeignKey_Edit field template as well as the
ForeignKey filter template populate their drop-down list controls. These methods return a
LINQ
IQueryable
object that you can use to retrieve rows from the database. Chapter 14,
“Implementing Security,” discusses how the
GetQuery
method can be overridden to imple-
ment row-level security.