[Entity Framework] Code First – Configuring Properties/Columns

In many cases, convention for properties does not come out the database structure that you want. So Code First allows you to override the convention by supplying additional configuration. You can do this in two ways: Data Annotations and Fluent API.

 

1. Required and Nullability

By default (convention),

  • Key properties: not null
  • Reference types: null
  • Value types: not null
  • nullable value types (Nullable<T>): null

If you apply the [Required] attribute to a property, it does two things:

  • Validates whether the field is not empty
  • Makes a database field “not null

“System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration” has the “IsRequired()” method.

  • public PrimitivePropertyConfiguration IsRequired()

 

2. Length

Length can be applied to string and an (byte) array. By convention, the length is “max“.

You can use:

  • [MaxLength] : sets the upper limit in the databse: nvarchar(n) or varbinary(n)
  • [MinLength] : sets the lower limit, model validation only, no effect to database

“System.Data.Entity.ModelConfiguration.Configuration.LengthPropertyConfiguration” has the “HasMaxLength()” method.

  • public LengthPropertyConfiguration HasMaxLength(Nullable<int> value)

Setting the value to null will remove any maximum length restriction.

 

3. Columns

The [Column] attribute is used how to configure the column for a property such as a name, the order, and a type.

  • public string Name { get; }
  • public int Order { get; set; }
  • public string TypeName { get; set; }

The “PrimitivePropertyConfiguration” class has the following methods:

  • public PrimitivePropertyConfiguration HasColumnName(string columnName)
  • public PrimitivePropertyConfiguration HasColumnOrder(Nullable<int> columnOrder)
  • public PrimitivePropertyConfiguration HasColumnType(string columnType)

 

4. Precision

When you use the “decimal” type in your model class, it maps to “decimal(18,2)” in SQL-server.

You can configure the precision only by Fluent API. The “DecimalPropertyConfiguration” class has the “HasPrecision()” method:

  • public DecimalPropertyConfiguration HasPrecision(byte precision, byte scale)

 

5. Data Annotation Example

public class Product
{
  public int Id { get; set; }

  [Required]
  public string Name { get; set; }

  [MaxLength(256)]
  public string Description { get; set; }

  [Column(TypeName = "image")]
  public byte[] Image { get; set; }
}

 

6. Fluent API Example

public class ProductContext : DbContext
{
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Product>().HasKey(p => p.Id).ToTable("MyProdutcs");
    modelBuilder.Entity<Product>().Property(p => p.Name).IsRequired();
    modelBuilder.Entity<Product>().Property(p => p.Description).HasMaxLength(256);
    modelBuilder.Entity<Product>().Property(p => p.Image).HasColumnType("image");
    modelBuilder.Entity<Product>().Property(p => p.Price).HasPrecision(10, 2);
  }
}

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