DbEntityEntry Class in Entity Framework

DbEntityEntry Class in Entity Framework

In this article, I am going to discuss DbEntityEntry Class in Entity Framework Database First Approach with Examples. Please read our previous article where we discussed How to Validate Entities in Entity Framework Database First Approach. We are going to work with the same example that we created in our Introduction to Entity Framework Database First Approach article. Please read our Introduction to Entity Framework Database First article before proceeding to this article.

DbEntityEntry Class in Entity Framework

The DbEntityEntry Class is one of the most important classes in Entity Framework. In Entity Framework, this Class is useful in retrieving various information about an entity. You can get an instance of the DBEntityEntry type of a particular entity by calling the Entry method on the DbContext object by passing the entity. For example:

DbEntityEntry studentEntry = context.Entry(entity);

The DbEntityEntry instance in Entity Framework allows us to access the entity state and the current and original values of all properties of a given entity. The following example shows how to retrieve important information about a particular entity by using the DbEntityEntry instance.

using System;
namespace DBFirstApproach
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of the context class
            using (EF_Demo_DBEntities context = new EF_Demo_DBEntities())
            {
                //Fetch the Student details whose StudentId is 1
                Student student = context.Students.Find(1);

                //Modify the Student First Name and Last Name
                student.FirstName = "FirstName Modified";
                student.LastName = "LastName Modified";

                //Get DbEntityEntry instance for the student entity
                var studentEntry = context.Entry(student);

                //Get entity information like Ful Name of the Entity
                Console.WriteLine($"Entity Ful Name: {studentEntry.Entity.GetType().FullName}");

                //Get the State of the Entity
                Console.WriteLine($"Entity State: {studentEntry.State}");

                Console.WriteLine("Entity Property Values");
                foreach (string propertyName in studentEntry.CurrentValues.PropertyNames)
                {
                    Console.WriteLine($"Property Name: {propertyName}");

                    //Get the Original Value of the Property
                    var OriginalValue = studentEntry.OriginalValues[propertyName];
                    Console.WriteLine($"\tOriginal Value: {OriginalValue}");

                    //Get the Current Value of the Property
                    var CurrentValue = studentEntry.CurrentValues[propertyName];
                    Console.WriteLine($"\tCurrent Value: {CurrentValue}");
                }
            }

            Console.Read();
        }
    }
}
Output:

DbEntityEntry Class in Entity Framework Database First Approach with Examples

In the next article, I am going to discuss Change Tracking in Entity Framework Database First Approach with Examples. Here, in this article, I try to explain DbEntityEntry Class in Entity Framework Database First Approach with Examples. I hope you enjoy this article.

Registration Open For New Online Training

Enhance Your Professional Journey with Our Upcoming Live Session. For complete information on Registration, Course Details, Syllabus, and to get the Zoom Credentials to attend the free live Demo Sessions, please click on the below links.

Leave a Reply

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