Entities in Entity Framework

Entities in Entity Framework

An entity in Entity Framework is a class in domain our application which is included as a DbSet<TEntity> type property in the derived context class. EF API maps each entity to a table and each property of an entity to a column in the database. In this article, I will discuss Entities in Entity Framework.

Please read Introduction to Entity Framework article of this article series. We are going to work with the same example that we created in the Introduction to Entity Framework article, so please read the Introduction to Entity Framework article before proceeding to this article.

In the Introduction to Entity Framework article, we created one console application which will retrieve the data from a SQL Server database using Entity Framework.

Let’s see the Solution of the console application that we created in the Introduction to Entity Framework article of this article series.

Entities in Entity Framework

The following Department and Employee classes are domain classes in this application.

namespace EFDemo
{
    using System;
    using System.Collections.Generic;
    
    public partial class Department
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Department()
        {
            this.Employees = new HashSet<Employee>();
        }
    
        public int ID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Employee> Employees { get; set; }
    }
}

namespace EFDemo
{
    using System;
    using System.Collections.Generic;
    
    public partial class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Gender { get; set; }
        public Nullable<int> Salary { get; set; }
        public Nullable<int> DepartmentId { get; set; }
    
        public virtual Department Department { get; set; }
    }
}

The above Department and Employee classes become entities when they are included as DbSet<TEntity> properties in the context class (the class which is derived from DbContext  Class).

Let’s open the EF_Demo_DBEntities.cs class which is a context class which is looks like below
namespace EFDemo
{
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    public partial class EF_Demo_DBEntities : DbContext
    {
        public EF_Demo_DBEntities()
            : base("name=EF_Demo_DBEntities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<Department> Departments { get; set; }
        public virtual DbSet<Employee> Employees { get; set; }
    }
}

In the above context class Departments and Employees, properties are of type DbSet<TEntity> which are called entity sets. The Departments and Employees are entities.

An Entity can include two types of properties: Scalar Properties and Navigation Properties.

Scalar Property:

The primitive type properties are called scalar properties. A scalar property stores the actual data. A scalar property maps to a single column in the database table.

For Example: In Employee class below are the scalar properties

public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public Nullable<int> Salary { get; set; }
public Nullable<int> DepartmentId { get; set; }

In Department Class Below are the Scalar properties

public int ID { get; set; }

public string Name { get; set; }

public string Location { get; set; }

Navigation Property:

The navigation property represents a relationship to another entity.

There are two types of navigation properties:

  1. Reference Navigation
  2. Collection Navigation
Reference Navigation Property:

If an entity includes a property of entity type, it is called a Reference Navigation Property. It represents the multiplicity of one (1).

For Example: In Employee Class below property is a Reference Navigation property.

public virtual Department Department { get; set; }

Collection Navigation Property:

If an entity includes a property of collection type, it is called a collection navigation property. It represents the multiplicity of many (*).

For Example: In Department class below property is a collection navigation property.

public virtual ICollection<Employee> Employees { get; set; }

In the next article, I will discuss the Types of entities in Entity Framework.

SUMMARY

In this article, I try to explain Entities in Entity Framework with one example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article

2 thoughts on “Entities in Entity Framework”

Leave a Reply

Your email address will not be published. Required fields are marked *