Telerik blogs
DotNetT2 Dark_1200x303

Learn how to use the shadow property (also known as the Navigation, Auditable or Tracking property) in Entity Framework Core.

Introduction

In this article, I will demonstrate how to use shadow property in Entity Framework Core, but before that we will see how we are using currently it. This article covers the following topics.

  • What is shadow property
  • The old way to use and access
  • The new way to use and access

What is Shadow Property?

The shadow properties in EF Core are not part of your entity class model but are part of your EF Core model and mapped with your database columns. Shadow property is also called as Navigation, Auditable or Tracking property. (See the Docs.)

The database table contains the shadow properties with names CreatedDate, DeletedDate, UpdatedDate, etc. It cannot be created with the data annotations.

The Old Way to Use and Access

In the old way we used to create a class or interface and inherited into our entity class and on the basis of that the tracking or navigation properties would be added into the database table as shown in the code below.

-- Database entity class with inherited tracking or auditable interface.

public class Shadow : ITracking
{
     public int Id { get; set; }
     public string Content { get; set; }
     public DateTime CreatedDate { get; set; }
}

-- Auditable or tracking interface we use for createddate, updateddate, etc.

public interface ITracking
{
    DateTime CreatedDate { get; set; }
}

-- Setting the navigation or tracking property value in our example, i.e. CreatedDate.

var model = new Shadow();
model.Content = "Old way to set";
model.CreatedDate = DateTime.Now; //setting the value

-- Getting the navigation or tracking property value in our example, i.e. CreatedDate.

var res = db.Shadows.FirstOrDefault();

if (res != null)
{
    Console.WriteLine("Added record Id :- {0}", res.Id);
    Console.WriteLine("Added record Content :- {0}", res.Content);
    Console.WriteLine("Added record CreatedDate - {0}", res.CreatedDate);//getting the value
}

The newly created database table Shadows with the tracking or navigation properties CreatedDate shown below uses the old way before Entity Framework Core:

The New Way to Use and Access

In the new way, meaning in EF Core, we don't need to create a separate class or interface and inherit in every database entity class. We need to add it in override OnModelCreating method of DbContext as shown in the code below.

-- In the EF Core we need only database entity class.

public class Article
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

-- For single or selected database entity, you can add navigation or shadow property as code below.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //creating navigation or shadow properties for single entity
    modelBuilder.Entity<Article>().Property<DateTime>("CreatedDate");
}

-- For multiple database entity, you can add navigation or shadow property as code below.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //creating navigation or shadow properties for all entity
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        modelBuilder.Entity(entityType.ClrType)
            .Property<DateTime>("CreatedDate");
    }
    base.OnModelCreating(modelBuilder);
}

-- Setting the navigation or tracking property value in our example, i.e. CreatedDate.

var nwArticle = new Article();
nwArticle.Name = "Shadow Property";
nwArticle.Description = "New way to set";
db.Entry(nwArticle).Property("CreatedDate").CurrentValue = DateTime.Now;
 //setting the value

-- Getting the navigation or tracking property value in our example, i.e. CreatedDate.

var res = db.Articles.FirstOrDefault();

if (nwFirstOrDefault != null)
{
    Console.WriteLine("Added record Id :- {0}", res.Id);
    Console.WriteLine("Added record Content :- {0}", res.Name);
    Console.WriteLine("Added record Description - {0}", res.Description);
    Console.WriteLine("Added record CreatedDate - {0}", db.Entry(res).Property("CreatedDate").CurrentValue); //getting the value
}

The newly created database table Articles with the tracking or navigation properties CreatedDate is shown below using the EF Core way:

You can also download this example from here.

Conclusion

In this article, we discussed what shadow property in Entity Framework Core is, the old way to use and how to access it, and the way using EF Core in a simple example. If you have any suggestions or queries regarding this article, please contact me.

“Learn it, Share it.”


jeetendra
About the Author

Jeetendra Gund

Jeetendra Gund is a C# Corner MVP as well as the Chapter Leader of C# Corner Pune Chapter. He holds a master’s degree in Computer Science and has worked on several different domains. He has spent six years in grooming his knowledge about Microsoft Technologies and has been sharing his experiences on technologies such as C#.Net, MVC.Net, SQL Server, Entity Framework, AngularJS, JavaScript, HTML, and .NET Static Code Analysis. He is a regular speaker at C# Corner on Microsoft Technologies. Find him: C# Corner, LinkedIn, or Twitter.

Comments

Comments are disabled in preview mode.