Data Access has been discontinued. Please refer to this page for more information.

Inserting Artificial Types

This article demonstrates how to work with artificial types runtime. You will see how to create new instances of artificial types and how to initialize them. To learn how to model artificial types by using Telerik Data Access Fluent Mapping API, see Mapping Artificial Types.

In this article:

Creating and Inserting Artificial Types with the CreateInstance Method

To create new instances of artificial types you can take advantage of the OpenAccessContext.CreateInstance extension method.

The OpenAccessContext class provides extension methods for creating new instances of artificial types. In order to use them, you need to use/import the Telerik.OpenAccess namespace.

When creating a new instance of an artificial type, you need to use the full name of the type (namespace + type name).

using ( FluentModelContext fluentContext = new FluentModelContext() )
{
   object product = fluentContext.CreateInstance( "FluentModel.Product" );
   object category = fluentContext.CreateInstance( "FluentModel.Category" );

   fluentContext.Add( product );
   fluentContext.Add( category );

   fluentContext.SaveChanges();
}
Using fluentContext As New FluentModelContext()
    Dim product As Object = fluentContext.CreateInstance("FluentModel.Product")
    Dim category As Object = fluentContext.CreateInstance("FluentModel.Category")

    fluentContext.Add(product)
    fluentContext.Add(category)

    fluentContext.SaveChanges()
End Using

Setting Properties of Artificial Types

There are several important notes that should be pointed here:

  1. Before performing any type of manipulation, you have to ensure that the target type is managed by the context. For example, when you create a new artificial type, you have to add it to the context before setting its properties.
  2. Next, when you want to assign a value to an artificial property you should use the SetFieldValue extension method.
  3. Finally, to commit the changes in the database, call the SaveChanges method.

When you want to set an artificial property of an artificial type, you have to ensure that the type is managed by the context. In the previous example, immediately after the creation of the artificial types they were added to the fluent context.

The SetFieldValue method is part of the Telerik.OpenAccess namespace.

using ( FluentModelContext fluentContext = new FluentModelContext() )
{
   object product = fluentContext.CreateInstance( "FluentModel.Product" );
   fluentContext.Add( product );
   product.SetFieldValue( "ProductName", "My Product" );
   product.SetFieldValue( "Price", 24 );

   object category = fluentContext.CreateInstance( "FluentModel.Category" );
   fluentContext.Add( category );
   category.SetFieldValue( "CategoryName", "MyCategory" );
   category.SetFieldValue( "Description", "New Description" );

   product.SetFieldValue( "Category", category );

   fluentContext.SaveChanges();
}
Using fluentContext As New FluentModelContext()
    Dim product As Object = fluentContext.CreateInstance("FluentModel.Product")
    fluentContext.Add(product)

    ' Extension methods on objects should be called as static methods
    ' due to limitations in VB
    Telerik.OpenAccess.ExtensionMethods.SetFieldValue(product, "ProductName", "My Product")
    Telerik.OpenAccess.ExtensionMethods.SetFieldValue(product, "Price", 24)

    Dim category As Object = fluentContext.CreateInstance("FluentModel.Category")
    fluentContext.Add(category)

    Telerik.OpenAccess.ExtensionMethods.SetFieldValue(category, "CategoryName", "MyCategory")
    Telerik.OpenAccess.ExtensionMethods.SetFieldValue(category, "Description", "New Description")

    Telerik.OpenAccess.ExtensionMethods.SetFieldValue(product, "Category", category)
    fluentContext.SaveChanges()
End Using

The following code shows you how to assign the category type to the Product.Category property.

product.SetFieldValue( "Category", category );
Telerik.OpenAccess.ExtensionMethods.SetFieldValue(product, "Category", category)

Creating and Inserting Artificial Types with the CreateDynamicInstance Method

The CreateDynamicInstance method creates a new instance of the specified persistence type and returns the instance as dynamic. To use the CreateDynamicInstance method you need to:

  • Add a reference to the Telerik.OpenAccess.40.Extensions.dll assembly.
  • Use the Telerik.OpenAccess namespace.

The following example illustrates how to work with the CreateDynamicInstance method. Again, before initializing your artificial type, you have to ensure that the type is managed by the context.

using ( FluentModelContext fluentContext = new FluentModelContext() )
{
   dynamic product = fluentContext.CreateDynamicInstance( "FluentModel.Product" );
   fluentContext.Add( product );
   product.productName = "My Product";
   product.price = 24;

   dynamic category = fluentContext.CreateDynamicInstance( "FluentModel.Category" );
   fluentContext.Add( category );
   category.categoryName = "MyCategory";
   category.description = "New Description";

   product.category = category;

   fluentContext.SaveChanges();
}
Using fluentContext As New FluentModelContext()
    fluentContext.UpdateSchema()
    Dim product As Object = fluentContext.CreateDynamicInstance("FluentModel.Product")
    fluentContext.Add(product)
    product.productName = "My Product"
    product.price = 24

    Dim category As Object = fluentContext.CreateDynamicInstance("FluentModel.Category")
    fluentContext.Add(category)
    category.categoryName = "MyCategory"
    category.description = "New Description"

    product.category = category

    fluentContext.SaveChanges()
End Using