Extend Sitefinity CMS data model: Create the fluent mappings
After you create the custom type class, you implement the fluent mappings that map your model classes to the database tables. To implement the fluent mappings for your data provider, you must create the following classes:
CustomMappingCustomMetadataSource
To use the mappings in the data provider, you must return an instance of the CustomMetaDataSource class in the GetMetaDataSource method of the provider.
Create the CustomMapping class
The CustomMapping class defines the mapping for the CustomType model class. To create the class:
- In Visual Studio, open the context menu of the CustomTypes project and click Add » Class.
- Name the class file
CustomMappings.csand click Add. - Make the
CustomMappingsclass inherit theOpenAccessFluentMappingBaseclass. - Create class constructor constructor.
- Implement the
OpenAccessFluentMappingBaseabstract class.
To implement the class, you must override theGetMappingabstract method. In the method, you create the mappings. You map theCustomTypeclass to thesf_custom_typetable and set the properties for each member.
Use the following code sample:
C#
using System.Collections.Generic;
using Telerik.OpenAccess.Metadata.Fluent;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Model;
namespace SitefinityWebApp
{
public class CustomMapping : OpenAccessFluentMappingBase
{
public CustomMapping(IDatabaseMappingContext context)
: base(context)
{
}
public override IList<MappingConfiguration> GetMapping()
{
var mappings = new List<MappingConfiguration>();
this.MapCustomType(mappings);
return mappings;
}
private void MapCustomType(IList<MappingConfiguration> mappings)
{
var customTypeMapping = new MappingConfiguration<CustomType>();
customTypeMapping.MapType(x => new { }).SetTableName("sf_custom_type", this.Context);
customTypeMapping.HasProperty(x => x.Id).HasFieldName("id").IsIdentity().IsNotNullable();
customTypeMapping.HasProperty(x => x.LastModified).ToColumn("last_modified").IsCalculatedOn(Telerik.OpenAccess.Metadata.DateTimeAutosetMode.InsertAndUpdate).IsNullable();
customTypeMapping.HasProperty(x => x.ApplicationName).HasFieldName("appName").ToColumn("app_name").HasLength(50).IsNullable();
customTypeMapping.HasProperty(x => x.Title).IsNullable();
mappings.Add(customTypeMapping);
}
}
}
Create the CustomMetaDataSource class
The CustomMetadataSourceclass wraps the mappings and exposes them to the OpenAccess data provider. To create the class:
- In Visual Studio, open the context menu of the
CustomTypesproject and click Add » Class. - Name the class file
CustomMetadataSource.csand click Add. - Make the
CustomMetadataSourceclass inherit thePagesMetadataSourceclass.
ThePagesMetadataSourceclass is inherits from theContentBaseMetadataSourceclass, which provides the basic mappings for the content, security, and workflow that need to be present in content based modules. - Create the class constructors.
- Override the
BuildCustomMappingsmethod. In the method, create an instance of theCustomMappingclass and add the instance to the default set of mappings.
C#
using System.Collections.Generic;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.Pages.Data;
namespace SitefinityWebApp
{
public class CustomMetadataSource : PagesMetadataSource
{
public CustomMetadataSource()
: this(null)
{
}
public CustomMetadataSource(IDatabaseMappingContext context)
: base(context)
{
}
protected override IList<IOpenAccessFluentMapping> BuildCustomMappings()
{
var mappings = base.BuildCustomMappings();
mappings.Add(new CustomMapping(this.Context));
return mappings;
}
}
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.