Open Xriuk opened 1 year ago
Also adding that the suggested approach should also track derived entities probably, so an operator like IN could be used, also it should handle other discriminator types like strings.
public enum EntityType{
None, // 0
Ecommerce, // 1
MyEcommerce1, // 2
MyEcommerce2, // 3
Logistics, // 4
Customer // 5
}
migrationBuilder.CreateIndex(
name: "IX_Entities_ApiId_Ecommerce",
table: "Entities",
column: "ApiId",
unique: true,
filter: "[Type] IN (1, 2, 3) AND [ApiId] IS NOT NULL");
Notes from triage: we should avoid creating any indexes here and warn indicating that uniqueness is not enforced by the database when sharing an FK column in this way. The user can then choose to create indexes manually for whatever their model actually is, rather than us attempting to infer that with a potentially partial discriminator mapping.
I think this a duplicate or at least closely related to https://github.com/dotnet/efcore/issues/25391
Note from triage: keeping this issue open to add a warning; https://github.com/dotnet/efcore/issues/25391 in case it ever gets implemented, which seems unlikely.
Include your code
Entities
Fluent API
This produces the following migration:
Which is fine I guess, because those columns would be null if the entity is not
Logistics
orEcommerce
but will be correctly populated on those types.But since I'm using TPH I'd like to save a column by mapping the
ApiId
to the same column:This produces the following migration:
As you can see the single index is not enough since I have two relationship on different entities types. I think something like this would work:
Maybe this would require some additional change tracking between migrations
Include provider and version information
EF Core version: 6.0.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0