2014-07-02 28 views

cevap

11
ekleyebileceğiniz size veri ek açıklama isterseniz

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

kullanılarak kompozit anahtar oluşturabilirsiniz

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

} 

adında bir varlık olduğunu varsayalım

İşte akıcı API aracılığıyla birleşik benzersiz anahtarın nasıl oluşturulacağını gösteren bir örnek. Birleşik anahtar, ProjectId ve SectionOdKey'den oluşur.

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

Let basit KeyAttribute birden özelliklere

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

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

} 
+0

Hey! Buna cevap vermek üzereydim. –

+0

@ByteBlast ahh! gr8 2 burada, hala eksik olan ans parçası, akıcı api ile benzersiz ekleyerek –

İlgili konular