0

時獲得的關鍵屬性我有一個是爲了設置其屬性的配置中使用EntityTypeConfiguration一個EF型號:我用DataAnnotations之前EF如何使用EntityTypeConfiguration

public EntityMap() 
     { 
      // Primary Key 
      this.HasKey(t => t.MasterEntity); 

      // Properties 
      this.Property(t => t.MasterEntity) 
       .IsRequired() 
       .HasMaxLength(10); 

      this.Property(t => t.EntityType) 
       .IsRequired() 
       .HasMaxLength(10); 

      this.Property(t => t.Description) 
       .IsRequired() 
       .HasMaxLength(50); 

      this.Property(t => t.GlobalEntity) 
       .HasMaxLength(10); 

      // Table & Column Mappings 
      this.ToTable("entity"); 
      this.Property(t => t.MasterEntity).HasColumnName("masterentity"); 
      this.Property(t => t.EntityType).HasColumnName("entitytype"); 
      this.Property(t => t.Description).HasColumnName("description"); 
      this.Property(t => t.GlobalEntity).HasColumnName("globalentity"); 
      this.Property(t => t.Active).HasColumnName("active"); 

,爲了得到主鍵我使用:

dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name; 

有關如何使用EntityTypeConfiguration時要做到這一點,但任何線索?

感謝

回答

0

您可以通過一個擴展方法我張貼here得到EntityKey

一旦你得到了EntityKey您可以通過

var keyNames = enityKey.EntityKeyValues.Select (ekv => ekv.Key) 
+0

格特得到的關鍵屬性名稱(S)我可以用同樣的方式來獲取映射表名(流利API)?欣賞 – VAAA

+0

不可以。EF 6.1有一個[公共映射API](https://entityframework.codeplex.com/wikipage?title=Public%20Mapping%20API),您可能想看看。否則,您可以很難達到這個目標:http://stackoverflow.com/a/20807366/861716。 –