5

Im trying to enforce unique constraint of a column using fluent configuration for code first. Is there any better way than implementing it in business logic or using something like below

context.ObjectContext.ExecuteStoreCommand("CREATE UNIQUE CONSTRAINT...");

Or I was wondering if it's not possible to implement unique constrain using fluent configurations?

EDIT :

It's confirmed that this is not possible with fluent configurations. But then again I'm wondering what's the best way to do this? And would like to know the reason behind EF not supporting this.

1

4 Answers 4

2

In this example you can see how to define a Unique key attribute and use it in any Entity.

http://code.msdn.microsoft.com/windowsdesktop/CSASPNETUniqueConstraintInE-d357224a

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply Imran. Actually it's a good way to do it. But in the end what it does is executing a vendor specific SQL statement, so in case I change my database to a different vendor I will end up modifying the code. But I do agree I will have to modify only one place.
1

I have this problem occurred before

it can not implement unique constrain using fluent API。

so, the way you used is correct!

1 Comment

thanks for the reply. But if I do it using a command it will be dependent of my database implementation. That's something I don't want to have.
1

This is possible with DbMigrations...

public override void Up()
{
    CreateIndex("dbo.MyTableName", "MyColumnName", unique: true); 
}

2 Comments

Yes indeed, I also have written a blog post on that you can find it here (geethanga.com/2013/11/adding-unique-constraints-entity.html). But my original question was if there's a way to do it with Fluent API.
The problem with putting custom code into migrations is that you risk losing that code when you decide to purge/combine/regenerate your migrations. I suggest keeping all your configurations in the EntityTypeConfiguration classes. Check my answer on how to it: stackoverflow.com/a/25779397/111438.
1

It can be done with a custom extension method:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration.Configuration;

internal static class TypeConfigurationExtensions
{
    public static PrimitivePropertyConfiguration HasUniqueIndexAnnotation(
        this PrimitivePropertyConfiguration property, 
        string indexName,
        int columnOrder = 0)
    {
        var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = true };
        var indexAnnotation = new IndexAnnotation(indexAttribute);

        return property.HasColumnAnnotation("Index", indexAnnotation);
    }
}

Check the full answer here: https://stackoverflow.com/a/25779348/111438

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.