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

Create a 'IsUnique' attribute #23066

Closed
BrunoBlanes opened this issue Oct 21, 2020 · 11 comments
Closed

Create a 'IsUnique' attribute #23066

BrunoBlanes opened this issue Oct 21, 2020 · 11 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@BrunoBlanes
Copy link

I would like to not need to type all of this just to set a few columns as unique in the database. This is just for one case, I have an entire set of 20 lines just to set unique constrains in my OnModelCreating method.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	base.OnModelCreating(modelBuilder);
	modelBuilder.Entity<Company>(company =>
	{
		company.HasIndex(x => x.IE).IsUnique();
		company.HasIndex(x => x.IM).IsUnique();
		company.HasIndex(x => x.CNPJ).IsUnique();
	});
}

Instead, why not create an [Unique] attribute to be used like this:

public class Company : Contact
{
	[Unique]
	[Required]
	[DataMember]
	public string CNPJ { get; set; }

	[Unique]
	[DataMember]
	public string? IE { get; set; }

	[Unique]
	[DataMember]
	public string? IM { get; set; }

	public Company() : base()
	{
		CNPJ = string.Empty;
	}
}

I don't believe this exists, given that I couldn't find any documentation that proved otherwise and also couldn't find any open issues on the matter. Please consider.

@smitpatel
Copy link
Contributor

Checkout IndexAttribute which allows you to configure indexes and market them as unique. This feature requires using EF Core 5.0. Current pre-release version on nuget, 5.0-rc2 contains it.

@BrunoBlanes
Copy link
Author

Thanks @smitpatel that does the job, but isn't it possible to have a simpler attribute per property? Or was there a design decision that said otherwise?

@ajcvickers
Copy link
Member

@BrunoBlanes Is it your intention to create a unique constraint or a unique index in the database, or does it not matter to you which of these is created?

@BrunoBlanes
Copy link
Author

You got me there, they seem to me to do the same thing, but if I studied properly what I want is a unique constraint, which is to have a single value per cell on a specific column.

@ajcvickers
Copy link
Member

@BrunoBlanes Thanks. So, to ask my question in a slightly different way: the reason you don't want to use [Index] is not because you care about the (largely academic) difference between a constraint and an index? (I believe most databases will create a unique index to implement the unique constraint anyway.)

@BrunoBlanes
Copy link
Author

BrunoBlanes commented Oct 23, 2020

It is not.
And it is no that I don't want to use it either, in fact I am using it now that you've showed me the attribute, but I think it is easier to understand which properties a specific column has on the database just by looking at its property on the model class and seeing the attributes on top of it.
In other words, if you have a model with 10 properties you want to set a unique constraint to, your [Index] attribute would be about two lines long on top of a class. I think it makes more sense to add it on a per property basis, and not globally.

@BrunoBlanes
Copy link
Author

[Index(nameof(IE), nameof(IM), nameof(CNPJ), IsUnique = true)]

This is what I had to do in one case for 3 properties, if I could have set one [Unique] attribute on top of each one of those properties it would make more sense for what I need.

@smitpatel
Copy link
Contributor

On side note,

[Index(nameof(IE), nameof(IM), nameof(CNPJ), IsUnique = true)]

Creates a unique index over all 3 properties (composite index). In order to define own separate unique index over each property following should be used

[Index(nameof(IE), IsUnique = true)]
[Index(nameof(IM), IsUnique = true)]
[Index(nameof(CNPL), IsUnique = true)]

@BrunoBlanes
Copy link
Author

Hum, that makes sense, I didn't read enough through the docs to see that, thanks @smitpatel.

@ajcvickers
Copy link
Member

We discussed this some more and created two new issues to cover points raised here: #23153 and #23154.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed type-enhancement labels Oct 30, 2020
@BrunoBlanes
Copy link
Author

@ajcvickers thanks for considering.

@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
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants