Databases Reference
In-Depth Information
The
GetTable
methods throws an
ArgumentException
if you specify a name or type of a
table that does not belong to the current
MetaModel
. In the unlikely scenario your code
expects to find non-existent tables, you can use the
TryGetTable
methods instead:
public bool TryGetTable(string uniqueTableName, out MetaTable table);
public bool TryGetTable(Type entityType, out MetaTable table);
The
TryGetTable
methods return a
Boolean
value that indicates whether or not the table
was found. The
MetaTable
itself is returned via the second output parameter. It will be
null
if the method returns
false
. This follows a common .NET pattern and mimics the
TryGetValue
method of the generic
Dictionary
class:
MetaTable table;
if (Global.DefaultModel.TryGetTable(“Products”, out table))
{
// use the table here …
}
Global Model Registration
By default, when a new
MetaModel
instance is created, it is registered globally, making it
available through the
Default
property of the
MetaModel
class as well as its
GetModel
method. Both of them are static, thus making the model available to any page or control
in your web application:
public static MetaModel Default { get; }
public static MetaModel GetModel(Type contextType);
The
Default
property gives you an application-independent way to access the default
model, which you would normally get to via the
DefaultModel
property of the Global
class defined in
Global.asax.cs
. In other words, in a typical application, the following
lines of code produce the same result:
MetaModel model = Global.DefaultModel;
MetaModel model = MetaModel.Default;
When multiple data contexts have been registered with Dynamic Data, you can also use
the static
GetModel
method of the
MetaModel
class to retrieve a globally registered
MetaModel
using its context type:
MetaModel model = MetaModel.GetModel(typeof(NorthwindEntities));
When using Dynamic Data in a large web application that interacts with multiple data-
bases and has multiple models, you might want to avoid global registration to reduce
chances of having different parts of the application affecting each other. For this purpose,
MetaModel
class provides an overloaded constructor that takes a
Boolean
value as a para-
meter. Passing
false
to this constructor prevents the model from being globally registered:
public MetaModel(bool registerGlobally);