Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Fluent API define unique constraint

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.

like image 331
Geethanga Avatar asked Mar 14 '13 13:03

Geethanga


2 Answers

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

like image 64
Imran Rashid Avatar answered Sep 23 '22 18:09

Imran Rashid


I have this problem occurred before

it can not implement unique constrain using fluent API。

so, the way you used is correct!

like image 45
czclk Avatar answered Sep 24 '22 18:09

czclk