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

Mapping Private Fields

This topic provides an example of how to map classes with private fields and how to create associations between them.

Mapping private fields is absolutely the same as mapping normal properties. In the following example the Product and Category classes contain only private fields. The specific moment is that you have to use the corresponding methods for private fields, e.g. HasPrimitiveField, HasStringField, HasAssociationField. Here is a sample FluentMetadataSource implementation demonstrating how to map the Product and Category classes and create an association between them.

You need to include/import the Telerik.OpenAccess.Metadata.Fluent.Advanced namespace.

public class FluentModelMetadataSource : FluentMetadataSource
{
   protected override IList<MappingConfiguration> PrepareMapping()
   {
       List<MappingConfiguration> configurations = new List<MappingConfiguration>();
       MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
       productConfiguration.MapType().ToTable( "Products" );
       productConfiguration.HasPrimitiveField( "id" ).IsIdentity( KeyGenerator.Autoinc ).
            ToColumn( "ProductId" );
       productConfiguration.HasStringField( "name" ).ToColumn( "ProductName" );
       productConfiguration.HasPrimitiveField( "categoryId" ).ToColumn( "CategoryId" );
       MappingConfiguration<Category> categoryConfiguration = new MappingConfiguration<Category>();
       categoryConfiguration.MapType().ToTable( "Categories" );
       categoryConfiguration.HasPrimitiveField( "id" ).IsIdentity( KeyGenerator.Autoinc ).
            ToColumn( "CategoryId" );
       categoryConfiguration.HasPrimitiveField( "name" ).ToColumn( "CategoryName" );
       productConfiguration.HasAssociationField( "category" ).OfType<Category>().
            WithOppositeField( "products" ).ToColumn( "CategoryId" );
       configurations.Add( productConfiguration );
       configurations.Add( categoryConfiguration );
       return configurations;
   }
}
Public Class FluentModelMetadataSource
    Inherits FluentMetadataSource
    Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
        Dim configurations As New List(Of MappingConfiguration)()
        Dim productConfiguration As New MappingConfiguration(Of Product)()
        productConfiguration.MapType().ToTable("Products")
        productConfiguration.HasPrimitiveField("id").IsIdentity(KeyGenerator.Autoinc). _
            ToColumn("ProductId")
        productConfiguration.HasStringField("name").ToColumn("ProductName")
        productConfiguration.HasPrimitiveField("categoryId").ToColumn("CategoryId")
        Dim categoryConfiguration As New MappingConfiguration(Of Category)()
        categoryConfiguration.MapType().ToTable("Categories")
        categoryConfiguration.HasPrimitiveField("id").IsIdentity(KeyGenerator.Autoinc). _
            ToColumn("CategoryId")
        categoryConfiguration.HasPrimitiveField("name").ToColumn("CategoryName")
        productConfiguration.HasAssociationField("_category").OfType(Of Category)(). _
            WithOppositeField("products").ToColumn("CategoryId")
        configurations.Add(productConfiguration)
        configurations.Add(categoryConfiguration)
        Return configurations
    End Function
End Class

The corresponding database tables will have the following structure:

Product Class

public class Product
{
   private int id;
   private string name;
   private int categoryId;
   private Category category;
}
Public Class Product
   Private id As Integer
   Private name As String
   Private categoryId As Integer
   Private _category As Category
End Class

Category Class

public class Category
{
   public Category()
   {
       this.products = new List<Product>();
   }
   private int id;
   private string name;
   private IList<Product> products;
}
Public Class Category
   Public Sub New()
       Me.products = New List(Of Product)()
   End Sub
   Private id As Integer
   Private name As String
   Private products As IList(Of Product)
End Class