Skip to content

Where property on IndexAttribute for Unique constraints #1698

Closed as not planned

Description

@rockstardev

Say that I have Attribute like this on my User object/table:

[Index("UNIQ_Nick", IsUnique = true)]
public string Nick { get; set; }

If I don't make Nick required (say I just want emails to register user) constraint will prevent future registrations because in SQL Server IsUnique constraint prevents rows with multiple

This is well described on:
http://stackoverflow.com/questions/767657/how-do-i-create-a-unique-constraint-that-also-allows-nulls
http://stackoverflow.com/questions/24361518/ef-6-1-unique-nullable-index

Do you have any plans to support something like:
[Index("UNIQ_Nick", IsUnique = true, Where="Nick IS NOT NULL")]

??

Would you accept pull request if I implemented this?

Activity

  1. rowanmiller commented on Feb 26, 2015

    @rowanmiller
    Contributor

    Hey,

    This isn't something we'd want to add to the attribute since it is very specific to relational databases. The best place to do this would be to edit the migration once it is scaffolded. We would consider a pull request to add support for this in the migrations index API.

    ~Rowan

  2. weitzhandler commented on Jul 21, 2015

    @weitzhandler
    Contributor

    @rowanmiller I can't seem to find the IndexAttribute attribute at all.
    #2675

  3. rowanmiller commented on Jul 21, 2015

    @rowanmiller
    Contributor

    @weitzhandler IndexAttrbiute was never part of DataAnnotations, it was something we had defined in our EF assembly. We may or may not have it in EF7 (there are some issues with the design we had in EF6 so we don't want to carry it over as-is). For the moment, you need to use the Fluent API to add indexes.

  4. AndriySvyryd commented on Jul 21, 2015

    @AndriySvyryd
    Member

    @weitzhandler The Index method is toplevel on the EntityTypeBuilder: modelBuilder.Entity<MyEntity>().Index(e => e.prop).Unique()

  5. weitzhandler commented on Jul 23, 2015

    @weitzhandler
    Contributor

    @rowanmiller

    I still miss the IndexAttribute anyway. I prefer attributes way over fluent. Especially when the model starts to become complex and the OnModelCreating becomes messy.
    BTW, there should be a way to implement an interface in any entity that provides an OnModelCreating called from the context's one, this way, the creation of the entity-specific attributes is kept within the entities.

    I understand about the IndexAttribute which is pretty technology-centric, but again, an attribute is just a useless thing on any DTO, so there is no real issue with annotating properties with technology specific attributes, given these attributes are part of any supported framework.

    Anyway thanks for your help @AndriySvyryd, anyway I haven't managed to find Order on the fluent API, is it around there?

  6. weitzhandler commented on Jul 24, 2015

    @weitzhandler
    Contributor

    How do I define the index order via fluent API?

  7. rowanmiller commented on Jul 29, 2015

    @rowanmiller
    Contributor

    @weitzhandler currently not possible. Feel free to open an issue to request it 😄

  8. weitzhandler commented on Jul 30, 2015

    @weitzhandler
    Contributor

    @rowanmiller
    But when creating a multi-key index, does it take care of the order automatically? By what precedence?

  9. rowanmiller commented on Jul 30, 2015

    @rowanmiller
    Contributor

    @weitzhandler oh do you mean the order of the columns rather than ASC/DESC? The order of columns is the order that you specify them in the API call to configure the index.

  10. yukozh commented on Oct 6, 2015

    @yukozh
    Contributor

    +1 for IndexAttribute

  11. applenele commented on Oct 25, 2015

    @applenele

    +1 for IndexAttribute

  12. weitzhandler commented on Oct 25, 2015

    @weitzhandler
    Contributor

    Yeah. Still hoping to see it back, I think it's not SQL-specific.
    Perhaps the attribute should be renamed to UniqueAttribute, which applies to ANY type of DB, even NoSql, and does resemble the concept of uniqueness rather than an Index which is tied up to specific vendor.

  13. yukozh commented on Oct 25, 2015

    @yukozh
    Contributor

    I don't think so. We often make a property as an index like a time field, They are able to be duplicated. Although we can make index and unique in modelbuilding, To implement an IndexAttribute is good for migrate from EF6 or earlier, and makes modeling easily.

  14. yukozh commented on Oct 25, 2015

    @yukozh
    Contributor

    Unique must be related to Index, because UniqueKey must be indexed.

  15. 7 remaining items

  16. joshcomley commented on Feb 5, 2016

    @joshcomley

    Figured it out:

            migrationBuilder.CreateIndex(
                name: ...,
                table: ...,
                column: ...,
                unique: true);
    
  17. joshcomley commented on Feb 5, 2016

    @joshcomley

    Or even better:

            migrationBuilder.AddUniqueConstraint(
                name: ...,
                table: ...,
                column: ...);
    
  18. ccpu commented on Jan 10, 2017

    @ccpu

    +1 for IndexAttribute

  19. tancevsasa commented on Apr 21, 2017

    @tancevsasa

    +1 for IndexAttribute

  20. Int32Overflow commented on May 30, 2017

    @Int32Overflow

    +1 for IndexAttribute

  21. zbrad commented on Jul 7, 2017

    @zbrad

    +1 for IndexAttribute

  22. JD-Robbs commented on Jul 26, 2017

    @JD-Robbs

    Adding my +1 for IndexAttribute.

  23. OronDF343 commented on Sep 18, 2017

    @OronDF343

    +1 for IndexAttribute

  24. AndriySvyryd commented on Oct 2, 2017

    @AndriySvyryd
    Member

    This issue is closed and will not be considered for planning purposes. Please direct all IndexAttribute feedback to #4050

  25. davidrevoledo commented on Oct 31, 2018

    @davidrevoledo

    +1 for IndexAttribute

  26. kanadaj commented on Dec 28, 2018

    @kanadaj

    This isn't something we'd want to add to the attribute since it is very specific to relational databases.

    image

    Given that there is a shared package just for relational databases, that seems like a very weak excuse not to do it.

  27. tidusjar commented on Jun 18, 2019

    @tidusjar

    Is there an update for this?

  28. ajcvickers commented on Jun 18, 2019

    @ajcvickers
    Contributor

    @tidusjar This issue is closed in favor of #4050, which is on the backlog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions