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

Where property on IndexAttribute for Unique constraints #1698

Closed
rockstardev opened this issue Feb 24, 2015 · 34 comments
Closed

Where property on IndexAttribute for Unique constraints #1698

rockstardev opened this issue Feb 24, 2015 · 34 comments

Comments

@rockstardev
Copy link

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?

@rowanmiller
Copy link
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

@weitzhandler
Copy link
Contributor

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

@rowanmiller
Copy link
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.

@AndriySvyryd
Copy link
Member

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

@weitzhandler
Copy link
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?

@weitzhandler
Copy link
Contributor

How do I define the index order via fluent API?

@rowanmiller
Copy link
Contributor

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

@weitzhandler
Copy link
Contributor

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

@rowanmiller
Copy link
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.

@yukozh
Copy link
Contributor

yukozh commented Oct 6, 2015

+1 for IndexAttribute

1 similar comment
@applenele
Copy link

+1 for IndexAttribute

@weitzhandler
Copy link
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.

@yukozh
Copy link
Contributor

yukozh commented Oct 25, 2015

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.

@yukozh
Copy link
Contributor

yukozh commented Oct 25, 2015

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

@ghost
Copy link

ghost commented Dec 8, 2015

@divega, @rowanmiller, wouldn't it make sense to keep this issue opened and add a label up for grabs. This looks pretty blunt in a wrong way to close the issue when clearly many consumers are asking for IndexAttribute feature. At least keep the discussion open to avoid duplicate issues (currently there are 5 closed issues on which people are +1'ing the same feature.. #107 (comment)).

@rowanmiller
Copy link
Contributor

@jasonwilliams200OK this issue was specifically about adding the ability to specify a 'WHERE' clause on an index via attributes. Agreed we have got a lot of feedback wanting an [Index] attribute, and we may introduce one based on that feedback. Looks like we don't have an issue tracking it yet, feel free to open one.

@ghost
Copy link

ghost commented Dec 11, 2015

@rowanmiller, should #2675 be reopened?

@rowanmiller
Copy link
Contributor

Added #4050 to the backlog

@SimonOrdo
Copy link

@AndriySvyryd

Is this still true, as of RC1?

"The Index method is toplevel on the EntityTypeBuilder:
modelBuilder.Entity().Index(e => e.prop).Unique()"

I can't find the Index method on EntityTypeBuilder. Is it an extension method in another namespace?

@rowanmiller
Copy link
Contributor

HasIndex is the method you are after - http://docs.efproject.net/en/latest/modeling/relational/indexes.html.

@joshcomley
Copy link

Is there any way in EF7 to add a unique constraint via migrations?

@joshcomley
Copy link

Figured it out:

        migrationBuilder.CreateIndex(
            name: ...,
            table: ...,
            column: ...,
            unique: true);

@joshcomley
Copy link

Or even better:

        migrationBuilder.AddUniqueConstraint(
            name: ...,
            table: ...,
            column: ...);

@ccpu
Copy link

ccpu commented Jan 10, 2017

+1 for IndexAttribute

3 similar comments
@tancevsasa
Copy link

+1 for IndexAttribute

@Int32Overflow
Copy link

+1 for IndexAttribute

@zbrad
Copy link

zbrad commented Jul 7, 2017

+1 for IndexAttribute

@JD-Robbs
Copy link

Adding my +1 for IndexAttribute.

@OronDF343
Copy link

+1 for IndexAttribute

@AndriySvyryd
Copy link
Member

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

@davidrevoledo
Copy link

+1 for IndexAttribute

@kanadaj
Copy link

kanadaj commented Dec 28, 2018

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.

@tidusjar
Copy link

Is there an update for this?

@ajcvickers
Copy link
Member

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

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests