Best EF Core Hosting Tutorial – Using Shadow Properties with Entity Framework Core


Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in C:\ClientSites\bestaspnethostingreview.com\httpdocs\wp-content\plugins\crayon-syntax-highlighter\crayon_formatter.class.php on line 119

BestASPNETHostingReview.com | Best and cheap Entity Framework core 1.1 hosting. Shadow Properties” is one of the new feature of Entity Framework Core. Shadow Properties are fields which are not part of your entity class so they don’t exist in your class but they do exist in entity model. In this post, let’s find out how to use shadow properties in Entity Framework Core.

Best and Recommended Entity Framework Core 1.1 Hosting Click Here !!

Use Shadow Properties with Entity Framework Core

find-the-best-image-2

The value and state of Shadow properties are maintained in the Change Tracker API. Shadow Properties are a useful in following conditions,

  • When you can’t make changes to existing entity class (third party) and you want to add some fields to your model.
  • When you want certain properties to be part of your context, but don’t wish to expose them.

So these properties are available for all database operations, but not available in the application as they are not exposed via entity class. Let’s see how to create and use them.

Creating Shadow Properties

For demonstration, let’s consider following entity class named Category. As per the domain class, there should be only 2 fields present in database Category table which are CategoryID and CategoryName.

And we wish to add a new field to database table named CreatedDate but as a shadow property. At the time of writing this post, you can only create Shadow Properties via Fluent API. So to create CreatedDate shadow property, override OnModelCreating and add the following code. Use Property() method to define the data type and name of the property.

As mentioned earlier, Change Tracker API is responsible for maintaining the shadow properties. So you can Get and Set value via Change Tracker API. Following code adds a category record in the database. See highlight line to set shadow property value

If you don’t specify any value for shadow properties, then DateTime.MinValue will be taken as default value. You can also set the value in SaveChanges() method. Following code, first finds all the entities with added state and then set the created date for every record.

How to refer them in LINQ query?

Shadow properties can be referenced in LINQ queries via the EF.Property static method. Following code, retrieves the category list order by created date and then display them.

To display or get the value, we need to use Change Tracker API only.

Shadow Property name matches with existing property name?

What happens when defined shadow property name matches one of the existing model property name? Is your code going to throw an exception? Well, no. The code will configure the existing property instead of creating a new shadow property. You can also configure column name to be different from Shadow Property. Like,

Summary

That’s it. Shadow properties may come handy while working with any existing entity and you don’t have access to modify it.