Domain Class
Domain Class
1. DataAnnotations
2. Fluent API
DataAnnotation:
DataAnnotation is a simple attribute based configuration, which you can apply on your
domain classes and its properties. You can find most of the attributes in the
System.ComponentModel.DataAnnotations namespace. However, DataAnnotation provides
only a subset of Fluent API configurations. So if you don't find some attributes in
DataAnnotation, then you have to use Fluent API to configure it.
[Table("StudentInfo")]
public class Student
{
public Student() { }
[Key]
public int SID { get; set; }
[Column("Name", TypeName="ntext")]
[MaxLength(20)]
public string StudentName { get; set; }
[NotMapped]
public int? Age { get; set; }
[ForeignKey("StdId")]
public virtual Standard Standard { get; set; }
}
Fluent API:
Fluent API configuration is applied as EF builds the model from your domain classes You
can inject the configurations by overriding the DbContext class's OnModelCreating method
as following:
public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base("SchoolDBConnectionString")
{
}
base.OnModelCreating(modelBuilder);
}
}
You can use modelBuilder, which is an object of DbModelBuilder class, to configure domain
classes.