Configurations in Entity Framework Core

Configurations in Entity Framework Core

In the previous tutorial I explained the Conventions in EF Core. If you want to override the conventions then apply the configurations and customize the EF model to database mappings.

There are 2 configuration methods in EF Core:

  • 1. Data Annotation Attributes
  • 2. Fluent API

Data Annotation Attributes

These are the .NET Attributes that can be applied to the domain classes and their properties.

The Data Annotation attributes are included in separate namespace System.ComponentModel.DataAnnotations.

Let me demonstrate how to apply these Data Annotation Attributes to the domain classes in order to configure database tables and override the default conventions of Entity Framework Core.

In the below code I have applied Data Annotation Attributes to the domain classes and their properties.

[Table("TBL_City")]
public class City
{
    [Key]
    public int KeyId { get; set; }

    [Column("CityName", TypeName = "varchar(25)")]
    public string Name { get; set; }

    [NotMapped]
    public int Population { get; set; }

    [ForeignKey("FKid")]
    public Country Country { get; set; }

}
[Table("TBL_Country")]
public class Country
{
    [Key]
    public int KeyId { get; set; }

    [MaxLength(20)]
    public string Name { get; set; }
}
public class CompanyContext : DbContext
{
    public DbSet<City> City { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=vaio;Database=Company;Trusted_Connection=True;");
        }
    }
}

Once you apply Migration you will get 2 database tables created with different names (due to [Table(“Name”)] attribute on the classes). These tables are:

1. TBL_City
2. TBL_Country

TBL_City

The TBL_City has:

  • KeyId column as primary key.
  • CityName column with data type varchar(25).
  • FKid as foreign key column.
Since the Population property has [NotMapped] attribute so no column is made for it on the TBL_City.

The TBL_Country has:

  • KeyId column as primary key.
  • Name column with data type nvarchar(20).

TBL City and TBL Country

Understanding how Data Annotation Attributes work

Attribute Description
Table Applied on entity class to give a name to database table.
Column Applied on a property to give column name, order and data type.
Key Sets the property as primary key for the table.
ForeignKey Applied to a property to mark it as foreign key.
NotMapped Can be applied to entity class or property for not generating a corresponding table or column in the database.
MaxLength Sets the max length for the table column.
Required Can be applied on properties to make the corresponding column on the table as not null.

Fluent API

Another way to configure domain classes is by using Entity Framework Fluent API. You will learn about Fluent API in the next chapter.

DOWNLOAD

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.