How to: Create A Model Based on MariaDB Database
This topic will walk you through the common task of creating a new Telerik Data Access model connected to a MariaDB database.
To use MariaDB with Telerik Data Access, make sure you have installed Connector/Net driver for MySQL. Telerik Data Access supports only the Connector/Net driver.
- Open Microsoft Visual Studio 2010 in elevated administrator mode.
- From the File menu, choose New and then Project.
- In the New Project dialog, expand the language of your preference. Select Windows and then select Class Library. Type GettingStartedMariaDb for a name of your project.
- Integrate the project with the Telerik.DataAccess.Fluent NuGet package.
-
In the App.config file, add a connection string to your MariaDB database:
<connectionStrings> <add name="MariaDbConnection" connectionString="Server=SERVERNAME;Database=DATABASENAME; Uid=USERNAME;Pwd=PASSWORD;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
Create POCO classes for the persistent classes.
- Using the Fluent Mapping API create the metadatasource class where the mapping is defined.
-
Create your context class:
-
Configure the backend to be MySql and to use the MySql.Data.MySqlClient provider.
private static BackendConfiguration backend = GetBackendConfiguration(); public static BackendConfiguration GetBackendConfiguration() { BackendConfiguration backend = new BackendConfiguration(); backend.Backend = "MySql"; backend.ProviderName = "MySql.Data.MySqlClient"; CustomizeBackendConfiguration(ref backend); return backend; }
Private Shared backend As BackendConfiguration = GetBackendConfiguration() Public Shared Function GetBackendConfiguration() As BackendConfiguration Dim backend As BackendConfiguration = New BackendConfiguration() backend.Backend = "MySql" backend.ProviderName = "MySql.Data.MySqlClient" CustomizeBackendConfiguration(backend) Return backend End Function
-
Add the name of the connection string:
private static string connectionStringName = @"MariaDbConnection";
Private Shared connectionStringName As String = "MariaDbConnection"
-
Create a new instance of your FluentMetadataSource class that holds the mapping:
private static MetadataSource metadataSource = new MyModelFluentMetadataSource();
Private Shared metadataSource As MetadataSource = New MyModelFluentMetadataSource()
-
Create a constructor for the context:
public MyModel() :base(connectionStringName, backend, metadataSource) { }
Public Sub New() MyBase.New(connectionStringName, backend, metadataSource) End Sub
-
Expose the persistent classes through read-only IQueryable<T> properties of the context:
public IQueryable<MyClass> MyClasses { get { return this.GetAll<MyClass>(); } }
Public ReadOnly Property MyClasses() As IQueryable(Of MyClass) Get Return Me.GetAll(Of MyClass)() End Get End Property
-