In this article, I am going to discuss Entities in Entity Framework with examples. Please read our previous article where we discussed Entity Framework Context Class in Detail. At the end of this article, you will understand what exactly an entity is and when and how to use Entities in Entity Framework.
What is an Entity in Entity Framework?
An entity in Entity Framework is a class in our application which is included as a DbSet type property in the derived context class. Entity Framework maps each entity to a table and each property of an entity to a column in the database.
We are going to work with the same example, that we worked in our previous article. Let’s see the Solution of the console application that we created in Introduction to Entity Framework article of this article series.
The following Department and Employee classes are domain classes in this application.
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
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. 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
The navigation property represents a relationship to another entity. There are two types of navigation properties:
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 the property is a collection navigation property.
public virtual ICollection Employees { get; set; }
In the next article, I am going to discuss Types of entities in Entity Framework. Here, 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
Summary:
I hope this post will be helpful to understand the concept of Entities in Entity Framework
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
What is an Entity in Entity Framework?
An entity in Entity Framework is a class in our application which is included as a DbSet
We are going to work with the same example, that we worked in our previous article. Let’s see the Solution of the console application that we created in Introduction to Entity Framework article of this article series.
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 have included as DbSet
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
Scalar Property:
The primitive type properties are called scalar properties. 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:
- Reference Navigation
- Collection Navigation
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 the property is a collection navigation property.
public virtual ICollection
In the next article, I am going to discuss Types of entities in Entity Framework. Here, 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
Summary:
I hope this post will be helpful to understand the concept of Entities in Entity Framework
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.