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.

Associations

M# allows you to quickly create different types of associations between your entities with a clean SQL schema and generated C# code.
It also gives you flexibility by using some specific Association attributes allowing you to change the behaviour in your C# logic or the generated SQL code.

Creation

Create a new entity for which you want to create an association with, for example "Status".

When typing the Status Type in our Employee Entity, M# displays a list of different association types:

  • Status - [* -> 1]: One to many association
  • Statuses - [* -> *]: Many to many association
  • Status - [1 -> 1]: One to one association

Different types of association

One to many

For creating a one to many association, select "Status - [* -> 1]" when you create the association property in M#.

M# will generate the following property for you:

and the following C# code:

If you carefully look at the C# code, you can notice that two properties have been generated for this association, one with the Type Guid? and the other with the type of the associated class Status.
The property

public Guid? StatusId { get; set; }

is used to find the associated instance in the database, in our case it corresponds to the unique ID of the Status class.
The Status property which returns the Status Type has a custom getter and setter.

  • Getter: searches in the database for a status with the ID corresponding to the property StatusId and returns null if there is not Status:
    return Database.Get(this.StatusId);
    .
  • Setter: gets the ID of the value (or gets null if the provided status is null) and then set it to the property StatusId:
    this.StatusId = value.Get(s => s.ID);
    .

As said previously the Status property calls the database each time we want to get its value and the value stored in the database is StatusId.

The database diagram highlights the association we just created in M#:

Many to many

For creating a many to many association, select "Statuses - [* -> *]" when you create the association property in M#.

M# will generate the following property for you:

and the following C# properties:

Instead of having a

Guid

and a

Status

type like for a one to many association, we now have an

IList<Guid>

and an

IEnumerable<Status>

.
M# will create the new SQL table "Employees_Statuses" for managing our many to many association as follow:

This new table will not be displayed as an entity in M#, the generated C# code provides us some methods to interact with this table and when we save our Employee entity the generated DAL will save all associated statuses.

Cloning an instance of an employee will also clone the list of attached statuses.

One to one

For creating a one to one association, select "Status - [1 -> 1]" when you create the association property in M#.

M# will generate the following property for you:

The generated SQL schema is exactly the same as for a One to many associations.
The generated C# is almost exactly the same, only a new static function to find an employee for a specific status is added and a new uniqueness rule:

Zero/One to x associations

You can convert your associations from 0 to 1 or 1 to 0 by setting the Mandatory attribute to True/False.
If it is 0 the generated property will be

public Guid? StatusId { get; set; }

and if it is 1

public Guid StatusId { get; set; }

.
The only difference in the C# code is that the Guid is nullable or not.

Create inverse

M# allows you to easily create an inverse association.
As an example, let's go back to our One to many association example. If we want to display the list of all employees for a status we click on the button "Create inverse...".

We are now editing the Status entity, if we start typing "Employees" M# will suggest creating the inverse association.

The generated inverse property will be a calculated list of Employee:

Once the inverse association is created, we can now call

myStatus.Employees

to get the list of all employees with the status "myStatus".

Attributes

Min cardinality

Min cardinality will have no effect on the database column definition.

M# will generate a new validation rule to make sure that your instance has at least two statuses:

Max cardinality

Max cardinality will have no effect on the database column definition.

M# will generate a new validation rule to make sure that your instance has not more than five statuses:

On delete

This attribute allows you to define a specific action when the associated instance is deleted.

Throw warning: This is the default behaviour of the association, the Status cannot be deleted if it is associated to an Employee and a ValidationException is thrown.
Cascade delete: If the Status is deleted all employees with this status will also be deleted.
Set to null: If the Status is deleted all employees associated to this Status will have a null status.
Call ReleaseXXX method: This will allow you to have the full control on the behaviour, in our example you will have to create the method

public void ReleaseStatus()

.