-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add discriminator filter for unique index on derived type #10665
Comments
@eduherminio Can you explain a bit more what the goal is here? If TPH is setup and a query is made for Company entities, then EF will only return Company entities, so it's not clear to me what the filter is for. I'm sure I must be missing something about what you are doing. Also, note that if you want the discriminator can be a normal mapped property of your entity type and can be accessed as such. By default, it is created as a shadow property, which can still be accessed in queries using the static |
Using this restriction I want Company (and only Company) names to be unique. modelBuilder.Entity<Company>()
.HasIndex(c => c.Name).IsUnique(); Due to TPH, and since the field As far as I can understand, this three restrictions would have the same effect nowadays: modelBuilder.Entity<Company>()
.HasIndex(c => c.Name).IsUnique();
modelBuilder.Entity<Department>()
.HasIndex(c => c.Name).IsUnique();
modelBuilder.Entity<Organization>()
.HasIndex(c => c.Name).IsUnique(); If I apply an index to Company (children) and not Organization (parent), I expect that uniqueness condition to affect only to Companies (although the field, I assume changing that behavior may not be easy (or even desirable), so the initial request was a simpler way to restrict constraints to children entities when applied to a parent's field. In other words, adding a condition to The filter can be used as a workaround to limit the restriction to some types of Organizations (just Companies, in the example), but it doesn't seem a straightforward way of configuring a restriction. |
@eduherminio Thanks for the additional info. We'll discuss this and get back. |
I thought I would add to this. I have a field in the database that needs to be a discriminator; part of the primary key of the table, and a text field of defined length and/or an integer constrained as a foreign key. I don't believe I can do this with the current fluent API. I have worked round this by adding a second column that is the key and a text field |
@MCFHTAGENTS This seems like a different scenario. Can you please file a new issue with a runnable project/solution or complete code listing that demonstrates the issue? |
#Devart , #Oracle modelBuilder.Entity() modelBuilder.Entity() This workaround is working fine with Sql Server but not with Oracle. I am using Devart.Data.Oracle.EfCore (9.11.980) with dotnet core 3.1 framework. Can u tell me soln. for Oracle with core 3.1 . |
@agrawalsaurabh21 it seems that Oracle does not support filtered indexes, but at the article shows, you can work around this with a function-based index that returns null. EF Core won't generate the DDL for creating this index, but you can use raw SQL in your migration to create it. |
@roji Below is the fix for the Oracle , we need to create a custom column in base table which will act as Discriminator and by doing so we can add this custom column to HasIndex method. Code : public string TableName { get; set; } modelBuilder.Entity().HasDiscriminator("TableName").HasValue("ChildTable1").HasValue("ChildTable2") modelBuilder.Entity().HasIndex(m => new { m.Name, m.DeletedOnUtc, m.TableName }).IsUnique().HasFilter(null); |
Issue description
Providing TPH & the following, simplified model:
OnModelCreating
: the following restriction is applied to all Organizations, so two Departments with the same name cannot be created.Workaround
We've found the following workaround, using a custom filter:
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
The text was updated successfully, but these errors were encountered: