efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Where property on IndexAttribute for Unique constraints

Open rockstardev opened this issue 11 years ago • 34 comments

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?

rockstardev avatar Feb 24 '15 23:02 rockstardev

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

rowanmiller avatar Feb 26 '15 20:02 rowanmiller

@rowanmiller I can't seem to find the IndexAttribute attribute at all. https://github.com/aspnet/EntityFramework/issues/2675

weitzhandler avatar Jul 21 '15 10:07 weitzhandler

@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.

rowanmiller avatar Jul 21 '15 17:07 rowanmiller

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

AndriySvyryd avatar Jul 21 '15 22:07 AndriySvyryd

@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 avatar Jul 23 '15 15:07 weitzhandler

How do I define the index order via fluent API?

weitzhandler avatar Jul 24 '15 07:07 weitzhandler

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

rowanmiller avatar Jul 29 '15 23:07 rowanmiller

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

weitzhandler avatar Jul 30 '15 00:07 weitzhandler

@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.

rowanmiller avatar Jul 30 '15 00:07 rowanmiller

+1 for IndexAttribute

yukozh avatar Oct 06 '15 23:10 yukozh

+1 for IndexAttribute

applenele avatar Oct 25 '15 02:10 applenele

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.

weitzhandler avatar Oct 25 '15 02:10 weitzhandler

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 avatar Oct 25 '15 02:10 yukozh

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

yukozh avatar Oct 25 '15 03:10 yukozh

@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.. https://github.com/aspnet/EntityFramework/issues/107#issuecomment-162855173).

ghost avatar Dec 08 '15 19:12 ghost

@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.

rowanmiller avatar Dec 08 '15 20:12 rowanmiller

@rowanmiller, should https://github.com/aspnet/EntityFramework/issues/2675 be reopened?

ghost avatar Dec 11 '15 09:12 ghost

Added https://github.com/aspnet/EntityFramework/issues/4050 to the backlog

rowanmiller avatar Dec 11 '15 18:12 rowanmiller

@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?

SimonOrdo avatar Dec 28 '15 20:12 SimonOrdo

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

rowanmiller avatar Dec 28 '15 21:12 rowanmiller