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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Geethanga
  • 1,836
  • 4
  • 20
  • 31

4 Answers4

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

Imran Rashid
  • 3,352
  • 4
  • 31
  • 41
  • 1
    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. – Geethanga Jul 13 '13 at 05:42
1

I have this problem occurred before

it can not implement unique constrain using fluent API。

so, the way you used is correct!

czclk
  • 157
  • 1
  • 6
  • 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. – Geethanga Mar 19 '13 at 14:03
1

This is possible with DbMigrations...

public override void Up()
{
    CreateIndex("dbo.MyTableName", "MyColumnName", unique: true); 
}
Ben Ripley
  • 2,115
  • 21
  • 33
  • Yes indeed, I also have written a blog post on that you can find it here (http://goo.gl/Ks9HZI). But my original question was if there's a way to do it with Fluent API. – Geethanga Feb 15 '14 at 05:06
  • 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: http://stackoverflow.com/a/25779397/111438. – niaher Sep 11 '14 at 05:02
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

Community
  • 1
  • 1
niaher
  • 9,460
  • 7
  • 67
  • 86