Shadow properties are properties that are not defined in the .NET entity class but are defined for that entity type in the EF Core model. The values and states of these attributes are maintained purely in the change tracker. Shaded properties are useful when data in a database should not be exposed on the mapped entity type. The Entity Framework core introduces a new type of attribute called the "shadow" attribute, which does not exist in EF 6.x.
The shadow attribute is not there. .NET entity classes. Instead, you can configure it for a specific entity type in the entity data model. It can be configured in the OnModelCreating() method of the context class.
The following diagram illustrates the shadow attribute.
As shown in the image above, the shadow attribute does not belong to your entity class. Therefore, you cannot access it when accessing other properties of an entity. Shaded properties can only be configured for entity types when building the entity data model, and they will also be mapped to database columns. The value and state of the shadow property are maintained only in the change tracker.
Let's understand the practical aspects of shadow properties. Let's say we need to maintain the creation and update dates of each record in the database table. You learned how to set the creation and modification dates for entities in EF Core by defining the CreatedDate and UpdatedDate properties in the entity class. Here we will see how to achieve the same result by using shadow properties instead of including shadow properties in the entity class.
Consider the following Student entity classes.
The Student class aboveThe CreatedDate and UpdatedDate attributes are not includedto maintain the time when it was created or updated. We configure them as shadow properties on the Student entity.
Define shadow properties
You can use the OnModelCreating() method to define shadow properties for entity types using the Fluent API.
The following configuration configures two shaded properties, CreatedDate and UpdatedDate, for the Student entity.
As you can see, the Property() method is used to configure the shadow properties. Specify the name of the shadow property as a string and the type as a generic parameter. If the name specified in the Property() method matches the name of an existing property, EF Core will configure that existing property as a shadow property instead of introducing a new shadow property.
Shadow properties in the database
Once the shadow properties are defined, we need to update the database schema as the shadow properties will be mapped to the corresponding database columns.
To do this, add a database migration in Visual Studio's Package Manager console using the following command.
The Student table will now include two columns, CreatedDate and UpdatedDate in SQL Server, as shown below.
So even if we don't include these properties in the Student class and configure them as shadow properties, the database will have the corresponding columns.
Access the shadow property
You can use the Property() method to get or set the value of the shadow property in EntityEntry. The following code accesses the value of the shadow property.
However, in our scenario, we want to automatically set the values to these shadow properties on the SaveChanges() method so that we don't have to manually set them on each entity object. So, override the SaveChanges() method in the context class as shown below.
This will automatically set values for the CreatedDate and UpdatedDate shadow properties.
Now, execute the following code and check the records in the database.
The above code will insert the following records in CreatedDate and UpdatedDate for Students.
So by configuring the shadow properties, we don't have to include them in the entity class.
Configure the shadow properties on all entities
Instead of manually configuring shadow properties on all entities at once, you can configure them manually.
For example, we can configure all entities of CreatedDate and UpdatedDate last time, as shown in the following image.
When to use the shadow property?
The shadow attribute can be used in two situations:
When you don't want to expose database columns on mapped entities, such as the scenario discussed above. When you don't want to expose foreign key attributes but only want to use navigation attributes to manage relationships. The foreign key property will be a shadow property and will be mapped to the database column, but will not be exposed as a property of the entity. (In EF Core, if you don't define a foreign key property in an entity class, it will automatically generate a shadow property for this.) You don't need to manually configure foreign key properties. )
Resources:
The hyperlink login is visible.
The hyperlink login is visible.
|