0% found this document useful (0 votes)
45 views2 pages

Domain Class

Code First uses conventions to build a conceptual model from domain classes. These conventions can be overridden using either data annotations or the fluent API. Data annotations use attributes applied directly to domain classes and properties, providing a subset of configuration options. The fluent API allows full configuration by overriding the OnModelCreating method to specify options using the model builder object. An example shows using data annotations like Key and ForeignKey to configure a Student class, while the fluent API overrides OnModelCreating to configure classes.

Uploaded by

suraj Rajbhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Domain Class

Code First uses conventions to build a conceptual model from domain classes. These conventions can be overridden using either data annotations or the fluent API. Data annotations use attributes applied directly to domain classes and properties, providing a subset of configuration options. The fluent API allows full configuration by overriding the OnModelCreating method to specify options using the model builder object. An example shows using data annotations like Key and ForeignKey to configure a Student class, while the fluent API overrides OnModelCreating to configure classes.

Uploaded by

suraj Rajbhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Code First builds conceptual model from your domain classes using default conventions.

Code first leverages a programming pattern referred to as convention over configuration. It


means you can override these conventions by configuring your domain classes to provide EF
with the information it needs. There are two ways to configure your domain classes.

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.

Following is an example of DataAnnotation used in Student Class:

[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; }

public int StdId { 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")
{
}

public DbSet<Student> Students { get; set; }


public DbSet<Standard> Standards { get; set; }
public DbSet<StudentAddress> StudentAddress { get; set; }

protected override void OnModelCreating(DbModelBuilder


modelBuilder)
{
//Configure domain classes using Fluent API here

base.OnModelCreating(modelBuilder);
}
}

You can use modelBuilder, which is an object of DbModelBuilder class, to configure domain
classes.

Let's see DataAnnotation

You might also like