-
Notifications
You must be signed in to change notification settings - Fork 36
Entity configuration
To configure entity you can use attributes or fluent configurator.
In most cases I try using a existing attributes used by Entity Framework.
Entity<Customer>.Add();
By default table name is a plural name of entity.
Customer => Customers
Product => Products
In both cases schema is optional and if not set it uses default db schema.
Attribute:
[Table("Customers", Schema = "dbo")]
public class Customer
Fluent:
Entity<Customer>.Add().SetTableName("Customers", "dbo");
By default is displaying all properties.
Attribute:
Columns attribute take a list of properties names.
[Columns("CompanyName", "ContactName", ...)]
public class Customer
Fluent:
Entity<Customer>.Add().SetColumns(c => c.CompanyName, c => c.ContactName, ...);
By default search are executed on all string and numeric properties.
Attribute:
Columns attribute take a list of properties names.
[Search("CompanyName", "ContactName", ...)]
public class Customer
Fluent:
Entity<Customer>.Add().SetSearchProperties(c => c.CompanyName, c => c.ContactName, ...);
Properties group are used in create or edit form. Properties are displaying in groups.
By default properties groups are empty.
Attribute:
In this case we need attribute on Entity and on properties.
In Groups attribute star after a group name means that a group by default is collapsed.
[Groups("Main section", "Contact section*")]
public class Customer
{
[Display(GroupName = "Main section")]
public string CompanyName { get; set; }
[Display(GroupName = "Contact section")]
public string ContactName { get; set; }
[Display(GroupName = "Contact section")]
public string ContactTitle { get; set; }
}
Fluent:
Entity<Customer>.Add()
.AddPropertiesGroup("Main section", c => c.CompanyName)
.AddPropertiesGroup("Contact section", true, c => c.ContactName, c => c.ContactTitle);
On create or edit form we want to see a list of foreign records, or on delete page we want to see what we could delete if we perform action. Off course we want to display a nice name for each record and to do tha we have a few options. Options are described from highest priority to lowest priority.
- Display format.
Set a display using a property name. You cannot do any operation on properties.
Attribute:
[RecordDisplay("{CompanyName} Id #{CustomerID}")]
public class Customer
Fluent:
Entity<Customer>.Add().SetDisplayFormat("{CompanyName} Id #{CustomerID}");
Sample result:
Around the Horn Id #AROUT
Berglunds snabbköp Id #BERGS
Bottom-Dollar Markets Id #BOTTM
- ToString() method.
In entity class just write a ToString() method. You can do anything you like with properties.
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public override string ToString()
{
return CompanyName.Take(5) + " Id #" + CustomerID;
}
}
Sample result:
Aroun Id #AROUT
Bergl Id #BERGS
Botto Id #BOTTM
- Automatic. Ilaro.Admin try automatic determine which property value display.
Ilaro.Admin search for property contains words (in that order): "Name", "Title", "Description", "Value".
If not found any value of primary will be displayed.
Attribute:
To set a primary key for entity you need add a attribute to property not to entity.
public class Customer
{
[Key]
public string CustomerId { get; set; }
}
Fluent:
Entity<Customer>.SetKey(c => c.CustomerId);
It used to display a friendly name of entity.
If not set Ilaro.Admin try determine singular and plural name.
Attribute:
[Verbose(Singular = "Customer", Plural = "Customers")]
public class Customer
Fluent:
Entity<Customer>.Add().SetDisplayName("Customer", "Customers");
Validation of data can only be done using Data Annotations on attributes of the Model. It relies on the native ASP.NET MVC implementation of Model validation.