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.

Properties

Properties define the structure of an entity and usually hold values.

Persisted properties hold data, meaning, it is used to store data permanently. When developer creates a persisted property, M# creates a corresponding SQL Table Column. For example, the illustration below has two persisted properties “First Name” and “Last Name”. Both of the properties are used to retrieve and store data permanently and correspond to a SQL Table "Column Names”.


M# will also generate the C# class code corresponding to your properties:


The SQL table:


M# allows you set specific attributes, changing the property definition e.g. from int to int? and defining validation rules.

General Attributes

Calculated

This attribute creates a read-only property in the generated C# file. The value is set by the Getter attribute.
Calculated attribute will have no effect on the database column definition.
Please refer to section Calculated properties for more details.

Default

This attribute allows you to specify a default value for the property.

Default attribute will have no effect on the database column definition, but will change your class constructor in the generated C# file.

Unique

This property ensures data for this column are unique in the database.
It has no effect on the database but will add a few things to the generated C# code.

Imagine the employee has a user account and can log into the system. If the username is the email address we have to make sure it is unique to distinguish users. If you set this attribute to "True", the validation method will make sure that the email address does not already exist in the system, if it exists a ValidationException will be thrown:


A cool feature generated by M# is the method FindByPropertyName, where PropertyName is the name of your unique property. Because an email is unique we can use this function to get the associated employee. If there is no employee

null

will be returned:

Attributes

Attributes allows you to specify custom attributes to the property, for example we can set the attribute

[Exportable]

if we want to generate this column in exports.
By using Reflection you will be able to get those attributes and perform operations.

Attributes will have no effect on the database column definition.

Column name

Column name attribute will have no effect on the generated C# file.
This will only change the column name in the database, this change is handled by the data access layer and does not change your entity file.
For example if you change the name from "Last name" to "Surname" you will continue to access the property by

Item.LastName

there is no impact in the application, only in the database.

Database index

Database index has no effect on the generated C# file. This attribute will create an index in the database for this column.
M# will often suggest you to specify an index on foreign key columns to improve the performance of SQL queries.

Default format string

Default format string attribute will have no effect on the database column definition or the generated C# class.
It allows you to display a custom expression in Views.
Example: The format "Employee {0}" will display "Employee Smith" if the value of the property is "Smith".

Documentation

Documentation attribute will have no effect on the database column definition or the generated C# class.
It adds a piece of information to your property XML documentation, for example the full name of the property, how to use it, how it works, ...

Format validation message

Format validation message will have no effect on the database column definition or the generated C# class.
It will generates a custom format validation message for the property.
For example, you create an Email property with the "Email" text pattern and you set a custom message. If the end user tries to validate an instance and the provided email address does not match the email pattern, the client-side validation will show a pop-up to the user with your custom message.

Getter

Specifies a custom getter for the property.
Refer to Calculated properties for more details.

Help text

Help text attribute will have no effect on the database column definition or the generated C# class.

This attribute generates a help icon next to the control in ASPX forms.

Is primary key

It is possible to specify the Primary Key Type at entity level. It supports String, Number as well as Guid (default).
If you define a Number or String, then you can optionally declare the property as the Primary Key, which will create a new unique column in the database as the primary key of the table. If left to default, it will add a property called ID.

Name

By setting a custom name, M# will change the name of your property and the name of your SQL column.
This change will not have any impact on the UI because the Name attribute is independent of the Title attribute.


Needs database type conversion

This attribute will have no effect on the database column definition.
It will generates a conversion in your generated Data Access Layer file for populating your instance from the database.
For example if this attribute is set to 'True' and you type is 'String', the code will be

entity.LastName = Convert.ToString(values[2]);

instead of

entity.LastName = (string)(values[2]);

.

Notes

Notes will have no effect on the database column definition or the generated C# class.
It is only used to display a note on a property to developers.

Override

Override will have no effect on the database column definition.
It allows you to extend or modify an inherited property in M#.
For example we have an abstract class 'Employee' defining a calculated 'FullName' property, which will be used by default for all 'Employee' instances:

Now we add the class 'DeveloperEmployee' with a different output for the property 'FullName', which will override the inherited property from Employee:

By doing this we can change the inherited propteries for this specific type of employee.

Property type

Allows you to set a custom type for a property, if this type is not a standard type (string, int, ...) and not a class in M#:

Required validation message

Required validation message will have no effect on the database column definition.
It generates a custom validation message if the user does not provide a value for the property:

Setter

Setter can be used with calculated properties only. By using this you can add a custom code to the setter before assigning the value.

Title

Title will have no effect on the database column definition or the generated C# class.
It will just use this value to display labels in UI forms / lists / views.

Validation group

Validation group will have no effect on the database column definition.
That will just add the

public override void ValidateProperties(string group)

method, which you can call to validate either a specific group of properties (i.e.

ValidateProperties('Info')

) or all properties.

Virtual

Refer to the 'Override' attribute section above for more details.