Add discriminator filter for unique index on derived type
@AndriySvyryd is already working on this.
Since Jan 12, 2018.
Assessment
This issue has not been assessed yet.
Description
Issue description
Providing TPH & the following, simplified model:
public class Organization
{
public string Name { get; set; }
}
public class Company : Organization { }
public class Department : Organization { }
OnModelCreating: the following restriction is applied to all Organizations, so two Departments with the same name cannot be created.
modelBuilder.Entity<Company>()
.HasBaseType<Organization>();
modelBuilder.Entity<Company>()
.HasIndex(c => c.Name).IsUnique();
Workaround
We've found the following workaround, using a custom filter:
modelBuilder.Entity<Organization>()
.HasDiscriminator<int>("Discriminator")
.HasValue<Company>(1)
.HasValue<Department>(2);
modelBuilder.Entity<Company>()
.HasIndex(c => c.Name).IsUnique()
.HasFilter("Discriminator IN (1)");
However, a more direct way of configuring it would be desirable.
Further technical details
EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 134
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100