[Entity Framework] Code First – Convention for Properties/Columns

Properties of Model classes are mapped to the table columns in DB. These mapping can be done by convention (default settings), and configurations using data annotations or Fluent API.

 

1. Model Class

The model class is just a POCO(Plain Old CLR Object) class. It does not need to be derived from the “EntityObject” class.

public class Product
{
  public int Id { get; set; }
  public string Name { get; set; }
  public decimal Price { get; set; }
  public decimal? DiscountPrice { get; set; }
  public int RegistrationNo { get; set; }
  public double? DiscountPercent { get; set; }
  public DateTime StartDate { get; set; }
  public DateTime? EndDate { get; set; }
  public bool IsTaxFree { get; set; }
  public byte[] Image { get; set; }
}

 

2. Context Class

To let EF know about the model classes, you need to configure the context object. In Code First, “System.Data.Entity.DBContext” is used. In EF, “System.Data.Objects.ObjectContext” is used.

public class ProductContext : DbContext
{
  public DbSet<Product> Products { get; set; }
}

 

3. Convention for Properties

  • The name of a column is the same as the name of a property.
  • The column name is not pluralized.

Depending on the type of a property, different convention will be applied.

 

4. Convention for Primary Keys

  • Looks for the propeties whose name is “Id” or <classname>Id
  • Matched columns are set to the primary key (PK, Not Null)
  • If the type of a property is “int“, the primary key column is configured to an “identity” column.

 

5. Convention for String Properties

  • String properties map to nullable text columns with no limit on length.
  • In SQL-Server, a string property maps to “navarchar(max), null

 

6. Convention for Value-Type Properties

By default, value types such as numbers may have values. Therefore value types map to the non-null fields.

In SQL-Server

  • The decimal type maps to “decimal(18, 2), not null
  • The int type maps to “int, not null
  • The double type maps to “float, not null

 

7. Convention for Nullable Value-Type Properties

C# supports nullable types. These nullable types become null columns.

  • The decimal? type maps to “decimal(18, 2), null
  • The int? type maps to “int, null
  • The double? type maps to “float,¬†null

 

8. Convention for Boolean Properties

A boolean type is a value-type, therefore it follows the rules for value types.

  • The bool maps to “bit, not null
  • The bool? maps to “bit, null

 

9. Convention for DateTime Properties

A DateTime type is also a value-type, therefore it follows the rules for value types.

  • The DateTime maps to “datetime, not null
  • The DateTime? maps to “datetime, null

 

10. Convention for Byte Array Properties

You can use a byte array to store binary data.

  • byte[] maps to “varbinary(max)¬†

 

11. Creating the Database

If you run the application and it creates a “Products” table.

One thought on “[Entity Framework] Code First – Convention for Properties/Columns

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s