Using Fluent Mapping API
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic demonstrates how to use the Telerik Data Access Fluent Mapping API to implement your Model. You will build a new Telerik Data Access Fluent Library project that will hold your domain model.
- Right-click the CarRentWebSite solution in Solution Explorer and select Add > New Project...
- In the list of Installed Templates on the left side of the dialog, select Visual C# or Visual Basic.
-
Then select Telerik Data Access Fluent Library. Name the project CarRentWebSite.Telerik Data Access and click OK.
-
This template works in the same way as the Telerik Data Access Class Library project template. The template will create a new class library project and will run the Telerik Data Access New Model Wizard. However, it will create a new model (from an existing database or an empty one) by using the fluent mapping code generation. The first step in the wizard lets you choose the type of model you want to use. In this step, you have the option of generating a model from a database or starting with an empty model. This topic deals with generating a model from an existing database, so select the Populate from Database option. Set the Model name to Entities Model and click Next.
The next page in the wizard lets you specify the data connection for your domain model. If you have previously created connections they will show up in the list. Create a data connection to the SofiaCarRental database and click Next.
- In the Choose Database Items screen, select all tables.
-
Click Finish to generate your model. Persistent classes are generated for all tables from the SofiaCarRental database. Also references to the Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll assemblies are added.
-
Open the EntitiesModel class and add the UpdateSchema method. This method migrates your database to the latest model state.
public void UpdateSchema() { var handler = this.GetSchemaHandler(); string script = null; try { script = handler.CreateUpdateDDLScript( null ); } catch { bool throwException = false; try { handler.CreateDatabase(); script = handler.CreateDDLScript(); } catch { throwException = true; } if ( throwException ) throw; } if ( string.IsNullOrEmpty( script ) == false ) { handler.ExecuteDDLScript( script ); } }
Public Sub UpdateSchema() Dim handler = Me.GetSchemaHandler() Dim script As String = Nothing Try script = handler.CreateUpdateDDLScript(Nothing) Catch Dim throwException As Boolean = False Try handler.CreateDatabase() script = handler.CreateDDLScript() Catch throwException = True End Try If throwException Then Throw End If End Try If String.IsNullOrEmpty(script) = False Then handler.ExecuteDDLScript(script) End If End Sub
Build your solution.
In the next task, you will add references to the required assemblies and connection string information in the CarRentWebSite project.