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.

Understanding Entity Types

The entity type is the fundamental building block for describing the structure of data within the Entity Data Model (EDM). In a conceptual model, an entity type represents the structure of top-level concepts, such as customers or orders. An entity type is a template for entity type instances. Each template contains the following information:

  • A unique name.
  • An entity key defined by one or more properties.
  • Data in the form of properties.
  • Navigation properties that allows navigation between associations.

In an application, an instance of an entity type represents a specific object (such as a specific customer or order). Each instance of an entity type must have a unique entity key within an entity set.
 

Defining Entity Types

As explained in the previous chapter, M# provides a very detailed and user friendly UI to define Entities and facilitates development of a Business Domain Model. M# fully supports and automates most of the tasks required to create associations between entities and generates SQL Tables and related .Net classes.

Attributes

Base type

This attribute allows you to specify a base type for the current entity. It is particularly useful when entities contain similar data, you don't need to define the same properties for all related entities, you can work with the base type instead of duplicating logic and attributes.
For example the base type of a "Developer" Entity or "Director" Entity could also be an "Employee" because they have a similar schema: Personal/Contact details, and share logic requirements e,g. if you need to store all login attempts you can use the generic "Employee".

By specifying a base class, M# implements inheritance among the entities and generates SQL database tables in a normalized fashion using foreign key relationships between tables using “ID” column. For more details please refer to tutorial Inheritance

Default property

Instead of specifying a custom ToString expression you can enforce M# to display the value of a property

Name

This is the name of the entity.

Instance accessors

By setting this to true M# will generates accessors for instances.
For example if you create a "Status" entity and set this to true you will be able to use:

myUser.Status = Status.Activated;

.

Sortable

If you want to set your entity as sortable, you first have to create the following property; Type = Integer, Name = "Order". Then set the following attributes; Sortable=True and Sort Property=Order.

The advantage of this attribute is that you don't have to worry about ordering, M# will do this for you. If you create a list of employees, you will be able to automatically generate up/down arrows and workflow for sorting.

Test instance name expression

If you are using the TDD M# template, you may have to specify this attribute. This attribute will allow you to specify a custom name to access instances in your Test project, by default M# uses the ToString() value, but you must to specify this attribute if ToString() is not unique.
If ToString() is the FirstName, you may have to set the expression to FullName and use it like this in your test project:

var johnSmith = World.Employees.JohnSmith;

.

Note that you have to use the keyword "each".

Class name

This is the name of the generated C# class.

Database mode

This allows you to specify a mode for the database. We will explain this in future chapters.

Documentation

Use this if you want to add a custom documentation for the generated class.
Default documentation:

Custom documentation:

Eager load data

Set this attribute if you don't want to defer the initialization of the object (lazy loading).

Generate parse method

This attribute will create a static function that returns the Employee instance that is textually represented by a specified string value, or null if no such object is found.


The search criteria is the ToString value of your instances.

Generate unit tests

Set this to false if you don't want to unit test this entity.

Is abstract

Set this attribute if you want to generate the class as abstract.

Is cachable

If you want to allow this type to be cached set the value to True, if you don't set it to False. If you don't specify a value M# will cache based on the value specified within the web.config attribute "Database.Cache.Enabled".

Is hierarchy

Set this object as hierarchical.
Please refer to this lesson for more details.

Is interface

Transforms your class to an Interface.
If your entity is an Interface you have to set the database mode to "Transient".

Log events

By default M# stores all events for all the instances, e.g. insert, update, delete. This attribute allows you to disable this behaviour.

Namespace

Specify the namespace of your entity.

Notes

This attribute has no impact on the generated C# or SQL code, it is only a note for developers.

Plural name

This is automatically generated by M#, this attribute allows you to define a custom value.

Primary key type

This is the type of the primary key. By default the type is a Guid and your entity implements the interface

GuidEntity

.

Schema

Schema of the database, "dbo" is the default value.

Soft delete

This attribute is very useful if you need to allow the user to delete data, but you want to retain a copy in the database. If you use Soft delete M# will create a hidden column in the SQL table:

[.Deleted] bit NOT NULL

.

If you call

Database.Delete()

M# will set this flag to "1" and you won't be able to get this instance by calling

Database.GetList()

Sort descending

If you set this to true M# will sort your data by descending when you will use

Database.GetList()

To string expression

Specify the value of ToString(). It is similar to the "Default property" attribute, except that you can write a custom code instead of using a persisted/calculated property.