2010-04-28 22 views
42

İlk önce EF4 kodunun üstesinden geliyorum ve şimdiye kadar beğeniyorum. Ancak, bir bileşimi bir bileşik birincil anahtarla bir tabloya eşleme konusunda sorun yaşıyorum.Bileşen birincil anahtarını önce Entity Framework 4 kodunda nasıl eşlerim?

denedim yapılandırma şuna benzer: Bu istisna atar

public SubscriptionUserConfiguration() 

    { 
       Property(u => u.SubscriptionID).IsIdentity(); 
       Property(u => u.UserName).IsIdentity(); 
    } 

: varlık türü 'SubscriptionUser' için bir anahtar anlaması edilemiyor.

Neyi eksik?

cevap

68

Ayrıca kullanabilirsiniz

HasKey(u => new { u.SubscriptionID, u.UserName }); 

Düzenleme:

public ProjectAssignmentConfiguration() 
{ 
    HasKey(u => u.Employee.EmployeeId); 
    HasKey(u => u.Project.ProjectId); 
} 

veya

: Ben bulduk

Bir sınırlama aşağıdaki işe kalmamasıdır

public ProjectAssignmentConfiguration() 
{ 
    HasKey(u => new { u.Employee.EmployeeId, u.Project.ProjectId }); 
} 

Peki, birleştirme tablosunun, yabancı anahtarlardan oluşan birincil anahtarın bulunduğu bir varlığı nasıl kurarsınız?

+2

Sadece bu aynı problemi yaşadı, semptomlarım doğru miktarda varlıkları geri getiriyordu, fakat aynı ilk anahtarı olan tüm varlıklar, ilk kaydı veri içeriğiyle döndürdüler, ikincisini görmezden geliyorlardı. Ayrıca benim ekler ile komik şeyler yapmak ve veritabanında zaten diğer kayıtların verilerini takmak gibi görünüyordu. – zclark

5

Çözüldü: Kimlik değil HasKey kullanmalıyım. Bu çalışır:

public SubscriptionUserConfiguration() 
{ 
    HasKey(u => u.SubscriptionID); 
    HasKey(u => u.UserName); 
} 
+0

kullanabilirsiniz Ya da belki Daniel önerdiği formunu kullanmak gereklidir? http://stackoverflow.com/questions/3299268/entity-framework-ctp4-and-composite-keys Bunun için iyi çalıştığını düşünüyorum. –

19

Ben şu Varlık

public class Account 
{ 
    public int AccountId1 { get; set; } 
    public int AccountId2 { get; set; } 
    public string Description { get; set; } 
} 
  1. kullanarak, adım adım anlatmaya çalışacağım kuralları geçersiz kılmak için EntityTypeConfiguaration<TEntity> Nesne türetilmiş bir sınıf oluşturun

    oluşturmak
    class AccountEntityTypeConfiguration : EntityTypeConfiguration<Account> 
    { 
    
        public AccountEntityTypeConfiguration() 
        { 
         // The Key 
         // The description of the HasKey Method says 
         // A lambda expression representing the property to be used as the primary key. 
         // If the primary key is made up of multiple properties then specify an anonymous type including the properties. 
         // Example C#: k => new { k.Id1, k.Id2 } 
         // Example VB: Function(k) New From { k.Id1, k.Id2 } 
         this.HasKey(k => new { k.AccountId1, k.AccountId2 }); // The Key 
    
         // Maybe the key properties are not sequenced and you want to override the conventions 
         this.Property(p => p.AccountId1).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); 
         this.Property(p => p.AccountId2).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); 
    
         this.Property(p => p.Description).IsRequired(); // This property will be required 
         this.ToTable("Account"); // Map the entity to the table Account on the database 
        } 
    } 
    
  2. DbContext Nesnesinden türetilen sınıf, OnModelCreating Yöntemini geçersiz kılar ve Conf'a yeni bir AccountEntityTypeConfiguration nesnesi ekler. model oluşturucunun igurasyonları.

    public class MyModelAccount : DbContext 
    { 
        public DbSet<Account> Accounts { get; set;} 
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder) 
        { 
         // Add a new AccountEntityTypeConfiguration object to the configuration of the model, that will be applied once the model is created. 
         modelBuilder.Configurations.Add(new AccountEntityTypeConfiguration()); 
        } 
    
    } 
    

size yardımcı olur umarım!

13

Ayrıca Column niteliğini

public class UserProfileRole 
{ 
    [Key, Column(Order = 0)] 
    public int UserId { get; set; } 

    [Key, Column(Order = 1)] 
    public int RoleId { get; set; } 
} 
+2

sadece bu arama için bir kafa up - ColumnAttribute gerektirir. NET 4.5 – zomf