RELATED VIDEOS |
In this video you will learn how to create an MVC 3 application using OpenAccess ORM Fluent Mapping API. In addition, you will learn how to work with the OpenAccessContext object in an MVC application.
 |
This topic demonstrates how to use the OpenAccess Fluent Mapping API to implement your Model. You will build a new OpenAccess 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 OpenAccess Fluent Library. Name the project CarRentWebSite.OpenAccess and click OK.

- This template works in the same way as the Telerik OpenAccess Class Library project template. The template will create a new class library project and will run the OpenAccess 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.
| C# |
Copy Code |
|
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 ); } } |
| VB.NET |
Copy Code |
|
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.