Skip to content
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

Model configuration: Ability to override IS NOT NULL clauses of an index #6794

Closed
tugberkugurlu opened this issue Oct 17, 2016 · 5 comments
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@tugberkugurlu
Copy link

This is for SQL Server and I am using EF Core 1.0.1.

Assume that I have the below entity:

public class Person
{
    public string Name { get; set; }
    public string Lastname { get; set; }
    public DateTime? DeletedOn { get; set; }
}

Inside OnModelCreating, I also have this to create a multi-column unique index:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>().HasIndex(x => new { x.Name, x.Lastname, x.DeletedOn }).IsUnique();
}

This causes EF Core to generate something similar to below:

CREATE UNIQUE NONCLUSTERED INDEX [IX_People_Name_Lastname_DeletedOn] ON [dbo].[People]
(
    [Name] ASC,
    [Lastname] ASC,
    [DeletedOn] ASC
)
WHERE ([Name] IS NOT NULL AND [Lastname] IS NOT NULL AND [DeletedOn] IS NOT NULL)

However, I want not to allow DeletedOn column to be null for this index. So, I am try to make EF Core to generate the below index creation script for me:

CREATE UNIQUE NONCLUSTERED INDEX [IX_People_Name_Lastname_DeletedOn] ON [dbo].[People]
(
    [Name] ASC,
    [Lastname] ASC,
    [DeletedOn] ASC
)
WHERE ([Name] IS NOT NULL AND [Lastname] IS NOT NULL)

QUESTION: Is this possible to do this with EF Core fluent API?

I dug a bit into the fluent API and was able to find some things related to HasAnnotation API but was not able to understand enough to achieve this.

@rowanmiller rowanmiller changed the title Question: how to create not nullable multi-column unique index? Model Definition: Ability to override IS NOT NULL clauses of an index Oct 24, 2016
@rowanmiller rowanmiller added this to the 1.2.0 milestone Oct 24, 2016
@rowanmiller
Copy link
Contributor

This is not possible. Assigning to 1.2 to look at providing this functionality.

@AndriySvyryd
Copy link
Member

Fixed in 428a328

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 4, 2016
@AndriySvyryd AndriySvyryd removed their assignment Nov 4, 2016
@ajcvickers ajcvickers changed the title Model Definition: Ability to override IS NOT NULL clauses of an index Model configuration: Ability to override IS NOT NULL clauses of an index May 9, 2017
@Davilink
Copy link

Davilink commented Feb 14, 2018

  1. Is it possible to have some example how to use it ? The documentation on https://docs.microsoft.com/en-us/ef/core/modeling/indexes doesn't explain that part.

  2. Is it possible that this can be back port to EF Core 1.0.5 ? Because i have that exact same issue, but i can't use EF Core 2.0, because i need to migrate my project to dotnet Core 2.0, and for the current moment is impossible. We have so much breaking change with the new mechanism of the Identity implementation.

@ajcvickers
Copy link
Contributor

@Davilink I filed dotnet/EntityFramework.Docs#610 for the docs. We will not be porting this fix to the 1.x versions of EF Core--the bar is very high to get patch changes into those releases, and this issue does not meet that bar.

@Davilink
Copy link

@ajcvickers Thx for the quick response.

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

5 participants