Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique key with EF code first

I have a following model in my project

public class Category {        public Guid ID { get; set; }     [Required(ErrorMessage = "Title cannot be empty")]     public string Title { get; set; } } 

and I'm trying to make Title as unique key, I googled for the solution, but couldn't find any. Can any suggest me how to do it, please?

like image 757
Prashant Cholachagudda Avatar asked Apr 18 '11 10:04

Prashant Cholachagudda


People also ask

How do I set primary key in Entity Framework?

Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys. In order to use composite keys, Entity Framework requires you to define an order for the key properties. You can do this by using the Column annotation to specify an order.


2 Answers

Unfortunately you can't define it as unique key in code first because EF doesn't support unique keys at all (it is hopefully planned for next major release). What you can do is to create custom database intializer and add unique index manually by calling SQL command:

public class MyInitializer : CreateDatabaseIfNotExists<MyContext> {   protected override void Seed(MyContext context)   {     context.Database.ExecuteSqlCommand("CREATE UNIQUE INDEX IX_Category_Title ON Categories (Title)");   } } 

And you must set this initializer in the bootstrap of your application.

Database.SetInitializer<MyContext>(new MyInitializer()); 

Edit

Now (EF 6.1 onwards )you can easily have unique constrains ,

[Index("TitleIndex", IsUnique = true)]  public string Title { get; set; } 
like image 58
Ladislav Mrnka Avatar answered Sep 24 '22 00:09

Ladislav Mrnka


First create the custom attribute class:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class UniqueAttribute : ValidationAttribute {    public override Boolean IsValid(Object value)     {         // constraint implemented on database         return true;     } } 

Then add to your classes:

public class Email {     [Key]     public int EmailID { get; set; }      public int PersonId { get; set; }      [Unique]     [Required]     [MaxLength(100)]     public string EmailAddress { get; set; }     public virtual bool IsDefault { get; set; }     public virtual Boolean IsApprovedForLogin { get; set; }     public virtual String ConfirmationToken { get; set; }      [ForeignKey("PersonId")]     public virtual Person Person { get; set; } } 

Then add a Initializer on your DbContext:

public class Initializer : IDatabaseInitializer<myEntities> {     public void InitializeDatabase(myEntities context)     {         if (System.Diagnostics.Debugger.IsAttached && context.Database.Exists() && !context.Database.CompatibleWithModel(false))         {             context.Database.Delete();         }          if (!context.Database.Exists())         {             context.Database.Create();              var contextObject = context as System.Object;             var contextType = contextObject.GetType();             var properties = contextType.GetProperties();             System.Type t = null;             string tableName = null;             string fieldName = null;             foreach (var pi in properties)             {                 if (pi.PropertyType.IsGenericType && pi.PropertyType.Name.Contains("DbSet"))                 {                     t = pi.PropertyType.GetGenericArguments()[0];                      var mytableName = t.GetCustomAttributes(typeof(TableAttribute), true);                     if (mytableName.Length > 0)                     {                         TableAttribute mytable = mytableName[0] as TableAttribute;                         tableName = mytable.Name;                     }                     else                     {                         tableName = pi.Name;                     }                      foreach (var piEntity in t.GetProperties())                     {                         if (piEntity.GetCustomAttributes(typeof(UniqueAttribute), true).Length > 0)                         {                             fieldName = piEntity.Name;                             context.Database.ExecuteSqlCommand("ALTER TABLE " + tableName + " ADD CONSTRAINT con_Unique_" + tableName + "_" + fieldName + " UNIQUE (" + fieldName + ")");                         }                     }                 }             }         }     } } 

And for last add the Initializer at Application_Start inside Global.asax.cs

System.Data.Entity.Database.SetInitializer<MyApp.Models.DomainModels.myEntities>(new MyApp.Models.DomainModels.myEntities.Initializer()); 

That's it. based on the vb code at https://stackoverflow.com/a/7426773

like image 26
Joao Leme Avatar answered Sep 21 '22 00:09

Joao Leme