Skip to content

Commit

Permalink
Document shared-type entity types
Browse files Browse the repository at this point in the history
Part of #3394
Fixes #3031
  • Loading branch information
AndriySvyryd committed Oct 25, 2021
1 parent cebe7f3 commit 4c1834f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 11 additions & 2 deletions entity-framework/core/modeling/entity-types.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Entity Types - EF Core
description: How to configure and map entity types using Entity Framework Core
author: roji
ms.date: 10/06/2020
author: AndriySvyryd
ms.date: 10/25/2021
uid: core/modeling/entity-types
---
# Entity Types
Expand Down Expand Up @@ -159,3 +159,12 @@ You can set an arbitrary text comment that gets set on the database table, allow
[!code-csharp[Main](../../../samples/core/Modeling/EntityTypes/FluentAPI/TableComment.cs?name=TableComment&highlight=4)]

***

## Shared-type entity types

> [!NOTE]
> Support for Shared-type entity types was introduced in EF Core 5.0.
Entity types that use the same CLR type are known as shared-type entity types. These entity types need to be configured with a unique name. Whenever a shared-type entity type is used, the supplied name needs to be provided in addition to the CLR type. This means that the corresponding `DbSet` property must be implemented using a `Set` call.

[!code-csharp[Main](../../../samples/core/Modeling/ShadowAndIndexerProperties/SharedType.cs?name=SharedType&highlight=3,7)]
16 changes: 13 additions & 3 deletions entity-framework/core/modeling/shadow-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Shadow and Indexer Properties - EF Core
description: Configuring shadow and indexer properties in an Entity Framework Core model
author: AndriySvyryd
ms.date: 10/09/2020
ms.date: 10/25/2021
uid: core/modeling/shadow-properties
---
# Shadow and Indexer Properties
Expand Down Expand Up @@ -50,15 +50,25 @@ Shadow properties cannot be accessed after a no-tracking query since the entitie

You can use the Fluent API to configure indexer properties. Once you've called the method `IndexerProperty`, you can chain any of the configuration calls you would for other properties. In the following sample, `Blog` has an indexer defined and it will be used to create an indexer property.

[!code-csharp[Main](../../../samples/core/Modeling/ShadowAndIndexerProperties/IndexerProperty.cs?name=ShadowProperty&highlight=3)]
[!code-csharp[Main](../../../samples/core/Modeling/ShadowAndIndexerProperties/IndexerProperty.cs?name=IndexerProperty&highlight=7)]

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 only be accessed via the indexer.

Indexer properties can be referenced in LINQ queries via the `EF.Property` static method as shown above or by using the CLR indexer property.

## Property bag entity types

> [!NOTE]
> Support for Property bag entity types was introduced 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, instead EF will create indexer properties. Currently only `Dictionary<string, object>` 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.
Entity types that contain only indexer properties are known as property bag entity types. These entity types don't have shadow properties, instead EF will create indexer properties. Currently only `Dictionary<string, object>` is supported as a property bag entity type. It must be configured as a [shared-type entity type](entity-types.md#shared-type-entity-types) with a unique name and the corresponding `DbSet` property must be implemented using a `Set` call.

[!code-csharp[Main](../../../samples/core/Modeling/ShadowAndIndexerProperties/SharedType.cs?name=SharedType&highlight=3,7)]

Property bag entity types can be used wherever a normal entity type is used, including as an owned entity type. However, they do have certain limitations:

- They can't have shadow properties.
- [Indexer navigations aren't supported](https://github.com/dotnet/efcore/issues/13729)
- [Inheritance isn't supported](https://github.com/dotnet/efcore/issues/9630)
- [Some relationship model-building API lack overloads for shared-type entity types](https://github.com/dotnet/efcore/issues/23255)
- [Other types can't be marked as property bags](https://github.com/dotnet/efcore/issues/22009)
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace EFModeling.ShadowAndIndexerProperties.IndexerProperty
{
#region IndexerProperty
internal class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region ShadowProperty
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().IndexerProperty<DateTime>("LastUpdated");
}
#endregion
}

public class Blog
Expand All @@ -27,4 +26,5 @@ public object this[string key]
set => _data[key] = value;
}
}
#endregion
}

0 comments on commit 4c1834f

Please sign in to comment.