Skip to content

Commit

Permalink
Add documentation for complete discriminator mapping
Browse files Browse the repository at this point in the history
Resolves #2231
  • Loading branch information
smitpatel committed Nov 9, 2020
1 parent 6d9ea0b commit 6f85a17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions entity-framework/core/modeling/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Finally, the discriminator can also be mapped to a regular .NET property in your

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/NonShadowDiscriminator.cs?name=NonShadowDiscriminator&highlight=4)]

When querying for derived entities which are part of TPH pattern, EF Core adds a predicate over discriminator column to get results for entity types which are included in the results. This filter makes sure that we don't get any additional row for base types or sibling types which is not part of the result. This filter predicate is skipped for base entity type since querying for base entity will get results for all the entities in the hierarchy. When materializing results from such a query, if we encounter a discriminator value which is not mapped to any entity type in the model, we throw an exception since we don't know how to materialize the results. This event only occurs if your database contains rows with discriminator values which are not mapped in EF Model. If you have such data then you can mark the discriminator mapping in EF Core model as incomplete to indicate that we should always add filter predicate for querying any type in the hierarchy. `IsComplete` API on discriminator configuration marks the mapping to be complete/incomplete based on bool value passed.

[!code-csharp[Main](../../../samples/core/Modeling/FluentAPI/DiscriminatorMappingIncomplete.cs?name=DiscriminatorMappingIncomplete&highlight=3)]

### Shared columns

By default, when two sibling entity types in the hierarchy have a property with the same name, they will be mapped to two separate columns. However, if their type is identical they can be mapped to the same database column:
Expand Down
29 changes: 29 additions & 0 deletions samples/core/Modeling/FluentAPI/DiscriminatorMappingIncomplete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;

namespace EFModeling.FluentAPI.DiscriminatorMappingIncomplete
{
public class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

#region DiscriminatorMappingIncomplete
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasDiscriminator()
.IsComplete(false);
}
#endregion
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}

public class RssBlog : Blog
{
public string RssUrl { get; set; }
}
}

0 comments on commit 6f85a17

Please sign in to comment.