Conventions in Entity Framework Core

Conventions in Entity Framework Core

Conventions in Entity Framework core are the default rules by which the Database schemas are created based on domain and context classes.

Let us consider the below domain and context classes to understand how conventions work in EF Core.

public class Employee
{
    public int Id { get; set; }
    public int DepartmentId { get; set; }
    public string Name { get; set; }
    public string Designation { get; set; }

    public Department Department { get; set; }
}
public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Employee> Employee { get; set; }
}
public class CompanyContext : DbContext
{
    public DbSet<Employee> Employee { get; set; }

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

Table

The EF Core will create database table, with the same name as the property, for all DbSet properties given in the context class.

It will also create tables for the domain class that are not included as DbSet properties but are reachable through reference properties in other domain class.

Here, database tables are created for both Employee and Department model classes. Employee model is included in the context class code property. While the Department domain is referenced in a property of the Employee domain class – public Department Department { get; set; }.

Column

EF Core creates columns in the database table for all the scalar properties in the domain classes. The column names are kept the same as the property name.

Reference and collection properties are used to create relationships between the tables.

C# data type to SQL Server column data type

The mapping of data types between C# and SQL Server are given below:

C# Data Type Mapping to SQL Server Data Type
int int
string nvarchar(Max)
decimal decimal(18,2)
float real
bool bit
long bigint()
datetime datetime
short smallint

Nullable Column

Null columns are created for all reference data types and nullable primitive type properties like string, Nullable, float?.

NotNull Column

Entity Framework Core will create NotNull columns in the database tables for primary key properties and primitive type properties like float, int, DateTime.

Primary Key

The EF Core creates the primary key for a database table for the property which is named Id or Id (case insensitive).

For the Employee table the EF Core will create primary key if the Employee domain class includes a property named id, ID, iD, Id, employeeid, EmployeeId, EMPLOYEEID, EmPLoyEEid, etc.

Foreign Key

The Entity Framework Core will create foreign key column for each reference navigation property in the domain class.

Here in my case (for ‘Employee’ & ‘Department’ domain classes) it will create foreign key column DepartmentId in the Employee table.

One-to-Many Relationship

Here you will learn how to apply One-to-Many Relationship between domain classes in Entity Framework Core.

Suppose you want to create 2 database tables – Country & City. You want to have One-to-Many relationship between them so that many cities are associated with one country.

These are the 2 Classes – Country & City, with the following properties.

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

    public string Name { get; set; }
}
public class City
{
    public int Id { get; set; }

    public string Name { get; set; }
}

There are 4 conventions to create a Many-to-One relationship between these 2 classes. These conventions are:

Convention 1: Create a Reference Navigation Property

On the ‘City’ class create a reference navigation property pointing to the ‘Country’ class as shown below:

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

    public string Name { get; set; }

    public Country Country { get; set; } //Reference Navigation Property
}
public class Country
{
    public int Id { get; set; }

    public string Name { get; set; }
}

On applying the Migration it will produce a one-to-many relationship (foreign key) between the City and Country tables in the database, where City table includes a nullable foreign key CountryId, as shown below.

one-to-many relationship ef core

Convention 2: Create a Collection Navigation Property

Another way to create one-to-many relationship is by adding collection navigation property ICollection in the Country class as shown below.

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

    public string Name { get; set; }

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

    public string Name { get; set; }

    public ICollection<City> Cities { get; set; } // Collection Navigation Property
}

This will do the same work as the convention 1.

Convention 3: Create both Reference & Collection navigation properties

You can also create both Reference & Collection navigation properties on the entities to create foreign key relationship (One-to-Many).

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

    public string Name { get; set; }

    public Country Country { get; set; } //Reference Navigation Property

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

    public string Name { get; set; }

    public ICollection<City> Cities { get; set; } // Collection Navigation Property

}

One the above code you can see that I have added Reference Navigation property on the City entity, and Collection Navigation property on the Country entity.

Convention 4: Use Convention 3 + foreign key property

Here you use the Convention 3 and also adds a foreign key property CountryId of type int on the City entity.

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

    public string Name { get; set; }

    public int CountryId { get; set; } //Foreign Key entity

    public Country Country { get; set; } //Reference Navigation Property

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

    public string Name { get; set; }

    public ICollection<City> Cities { get; set; } // Collection Navigation Property
}

One-to-One Relationship

Creating One-to-One relationship between entity classes is very simple in EF Core. All you have to do is add Reference Navigation properties on both the entities.

In the below code I have created One-to-One Relationship between the ‘Country’ & ‘City’ entity classes.

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

    public string Name { get; set; }

    public Country Country { get; set; } //Reference Navigation Property

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

    public string Name { get; set; }

    public City City { get; set; } //Reference Navigation Property
}

one-to-one relationship ef core

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.