From 7aa456ec7574380dab40485dedf12f312f760ce8 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 10 Nov 2020 13:46:30 -0800 Subject: [PATCH] Add doc for indexer property Resolves #2018 --- .../core/modeling/shadow-properties.md | 14 +++++++-- .../Modeling/FluentAPI/IndexerProperty.cs | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 samples/core/Modeling/FluentAPI/IndexerProperty.cs diff --git a/entity-framework/core/modeling/shadow-properties.md b/entity-framework/core/modeling/shadow-properties.md index 36fa5eb569..4d1e3b13f6 100644 --- a/entity-framework/core/modeling/shadow-properties.md +++ b/entity-framework/core/modeling/shadow-properties.md @@ -7,7 +7,9 @@ uid: core/modeling/shadow-properties --- # Shadow and Indexer Properties -Shadow properties are properties that are not defined in your .NET entity class but are defined for that entity type in the EF Core model. The value and state of these properties is maintained purely in the Change Tracker. Shadow properties are useful when there is data in the database that should not be exposed on the mapped entity types. +Shadow properties are properties that aren't defined in your .NET entity class but are defined for that entity type in the EF Core model. The value and state of these properties is maintained purely in the Change Tracker. Shadow properties are useful when there's data in the database that shouldn't be exposed on the mapped entity types. + +Indexer properties are entity type properties, which are backed by [indexer](/dotnet/csharp/programming-guide/indexers/) in .NET entity class. They can be accessed using indexer on the .NET class instances. It also allows you to add additional properties in the entity type model without changing the CLR class. If the CLR type has indexer defined, then when creating shadow properties conventionally, EF Core by default creates an indexer property. ## Foreign key shadow properties @@ -44,11 +46,19 @@ var blogs = context.Blogs Shadow properties cannot be accessed after a no-tracking query since the entities returned are not tracked by the change tracker. +## Configuring indexer properties + +You can use the Fluent API to configure indexer properties. Once you've called the string `IndexerProperty`, you can chain any of the configuration calls you would for other properties. In the following sample, `Blog` has indexer defined and it will be used to create indexer property. + +[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/IndexerProperty.cs?name=ShadowProperty&highlight=3)] + +If the name supplied to the `IndexerProperty` method matches the name of an existing indexer property, then the code will configure that existing property. If the entity type has a property, which is backed by a property on the entity class, then an exception is thrown since indexer properties must be accessed via indexer only. + ## Property bag entity types > [!NOTE] > Support for Property bag entity types was added in EF Core 5.0. -Entity types that contain only indexer properties are known as property bag entity types. These entity types don't have shadow properties. Currently only `Dictionary` is supported as a property bag entity type. This means that it must be configured as a shared entity type with a unique name and the corresponding `DbSet` property must be implemented using a `Set` call. +Entity types that contain only indexer properties are known as property bag entity types. These entity types don't have shadow properties. Currently only `Dictionary` is supported as a property bag entity type. It must be configured as a shared entity type with a unique name and the corresponding `DbSet` property must be implemented using a `Set` call. [!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/SharedType.cs?name=SharedType&highlight=3,7)] diff --git a/samples/core/Modeling/FluentAPI/IndexerProperty.cs b/samples/core/Modeling/FluentAPI/IndexerProperty.cs new file mode 100644 index 0000000000..21b3291b58 --- /dev/null +++ b/samples/core/Modeling/FluentAPI/IndexerProperty.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; + +namespace EFModeling.FluentAPI.IndexerProperty +{ + class MyContext : DbContext + { + public DbSet Blogs { get; set; } + + #region ShadowProperty + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().IndexerProperty("LastUpdated"); + } + #endregion + } + + public class Blog + { + private readonly Dictionary _data = new Dictionary(); + public int BlogId { get; set; } + public object this[string key] + { + get => _data[key]; + set => _data[key] = value; + + } + } +}