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.

Property Uniqueness

This tutorial explains about implementing a uniqueness rule on a single entity property. M# allows having any number of unique properties in an entity by marking an attribute “Unique” on a property e.g. Email

M# generates a method for each property which is marked as unique, to find a record in the database based on the property value. In our case, “FindByEmail” method is generated by marking “Email” property as unique in “User” entity.

A uniqueness validation is also applied on the property marked as unique in the “ValidateProperties” method of the entity class

Note: Database.Any() method is called to enforce the uniqueness rule, not “FindByEmail” method, which internally calls Database.Find(). That is because the purpose here is to just check the existence of a user with the same email address not to hold reference of the user instance.