2014-07-02 10 views

Odpowiedz

11

Oto przykład pokazujący, jak utworzyć złożony klucz unikalny za pomocą płynnego interfejsu API. Klucz złożony składa się z ProjectId i 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})); 
} 
13

powiedzmy masz podmiot o nazwie

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

} 

można utworzyć klucz kompozytowego za pomocą

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}); 
    } 
} 

jeśli wolisz adnotacji danych można prosty dodać KeyAttribute do wielu właściwości

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

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

} 
+0

Hej! Właśnie miałem odpowiedzieć na to pytanie. –

+0

@ByteBlast ahh! gr8 2 tutaj, część ans wciąż brakuje, dodając unikalne poprzez płynne API –

Powiązane problemy