M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Working with Existing Database

So far in the previous tutorials we have worked with managed entities (Entities for which a database table is generated and mapped using the data access logic class). Sometimes we also need to work on an existing database and we just need to map the entity to a database table for data handling. In this tutorial, we will see how we can work with external databases in M#.

In tutorial Entity, Page, Module, we discussed database modes while explaining entity management. Database mode is configurable at the time of creating an entity and also can be changed using entity attributes (Please read tutorial Understanding Entity Types for more details on entity attributes).

When you need to deal with an existing database and want to generate ado.net data accessor but not the database table itself then you must select “Existing” database mode when creating an entity in M#, as shown below:

On selecting “Existing” as database mode, M# generates the entity classes and ADO.Net data accessor, as for a managed entity but no database table is generated. The screenshot below shows that an entity is created with the provided database mode and we have added a property “Name” (Please read tutorial Properties for creating a property)

M# generates the Partial entity class and allows developers to generate the partial logic class for writing business rules (Please read tutorial Partial Classes & Business Logic for more details on working with partial classes).

M# also generates a data access logic (DAL) class for the created entity to map properties to the database columns enabling data access functionality.

Important: Persisted properties of this type of entity must match the existing database table columns, because M# generates data access logic and maps properties to database columns.

If you want to create an entity without Ado.Net accessor you should select “Custom” as the database mode, as shown below:

For custom database mode type entities you must write your data access logic. You should only use this database mode when you need to communicate with an external database. If you do not have an external database and want to create an entity to perform complex business logic then you should consider “Transient” entity types (Transient entities are explained more in tutorial Transient, Abstract, Interface)