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.

Combined Property Uniqueness

The previous tutorial explained Property Uniqueness for individual properties of an entity. In this tutorial we will work on combinations of properties to define a uniqueness rule. M# allows you to add any number of uniqueness rules and you may use one property in multiple combinations. Combined property uniqueness rules are maintained in a separate section called “Uniqueness Rules” just after the “Properties” section of an entity. M# displays a list of all the available combinations based on the entity properties which can be edited by adding or removing properties as required.

For the purpose of this tutorial, let’s say, we have two entities “Category” and “Product” with a many to many relationship. We have a bridging entity, which has a relationship between a category and a product and we want to restrict duplicates of a products in the same category (For more information on creating entitles and association, please read Entities, Module and Associations).

In this scenario, we cannot use Property Uniqueness on product in our bridging entity because that will restrict adding the same product in other categories as well. Now here lies the concept of “Combined Property Uniqueness” where we can add a uniqueness rule on combination of properties (category and product) in our bridging entity. This combined uniqueness rule will eradicate the duplication of product in same category but we will be able to add the same product to a different categories.

M# generates a static method for each uniqueness rule, which is named based on the properties selected. This method should be called to get a record in the database, which requires selected properties as method arguments. In our case, “FindByCategoryAndProduct” method is generated based on the uniqueness rule “Category and Product”.

A uniqueness validation rule is also generated in the “ValidateProperties” method of the entity class to impose a unique product and category relationship.