Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a composite unique key using EF 6 Fluent Api?

I have a table (Id, name, itemst, otherproperties), Id is the primary key and I want a unique composite key (name, itemst). How can I add this using code first either by fluent API (preferred) or annotation?

like image 745
Bargitta Avatar asked Jul 02 '14 04:07

Bargitta


People also ask

How do I add a composite primary key in Fluent API?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.

How do I create a composite key in Entity Framework?

You can also configure multiple properties to be the key of an entity - this is known as a composite key. Composite keys can only be configured using the Fluent API; conventions will never set up a composite key, and you can not use Data Annotations to configure one.

How do I add a foreign key in Fluent API?

You can then configure foreign key properties by using the HasForeignKey method. This method takes a lambda expression that represents the property to be used as the foreign key.

What is composite unique key?

A composite unique key is a unique key made up of a combination of columns. Oracle creates an index on the columns of a unique key, so a composite unique key can contain a maximum of 16 columns. To define a composite unique key, you must use table_constraint syntax rather than column_constraint syntax.


2 Answers

Let say you have a entity named

public class MyTable
{
    public int Id {get; set;}
    public String Name {get; set;}

}

you can create composite key using

public class YourContext : DbContext
{
    public DbSet<MyTable> MyTables { get; set; }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
        builder.Entity<MyTable>().HasKey(table => new {table.Id, table.Name});
    }
}

if you prefer data annotation you can simple add KeyAttribute to multiple properties

public class MyTable
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]  // optional
    [Key]
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.None)]   // optional
    [Key]
    public String Name { get; set; }

}
like image 113
Parimal Raj Avatar answered Nov 09 '22 06:11

Parimal Raj


Here is an example showing how to create a composite unique key via fluent API. The composite key consists of ProjectId and SectionOdKey.

public class Table
{
    int Id{set;get;}    
    int ProjectId {set;get;}
    string SectionOdKey{set;get;}
}

public class TableMap : EntityTypeConfiguration<Table>
{
   this.Property(t => t.ProjectId).HasColumnName("ProjectId")
                .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_ProjectSectionOd", 1){IsUnique = true}));
   this.Property(t => t.SectionOdKey).HasColumnName("SectionOdKey")
                .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_ProjectSectionOd", 2){IsUnique = true}));
}
like image 44
Bargitta Avatar answered Nov 09 '22 06:11

Bargitta