3

I am using c#, with EF 5 and MVC4 I am using EntityTypeConfiguration to set up the primary key and the backend table

  public class AuditZoneMap : EntityTypeConfiguration<AuditZone>
    {
        public AuditZoneMap()
        {
            // Primary Key
            HasKey(t => t.Id);


            // Properties
            Property(t => t.Description)
                .HasMaxLength(100);


            // Table & Column Mappings
            ToTable("AuditZone");
            Property(t => t.Id).HasColumnName("Id");
            Property(t => t.Description).HasColumnName("Description");
            Property(t => t.Valid).HasColumnName("Valid");          
            Property(t => t.CreatedDate).HasColumnName("CreatedDate");
            Property(t => t.CreatedBy).HasColumnName("CreatedBy");
            Property(t => t.ModifiedDate).HasColumnName("ModifiedDate");
            Property(t => t.ModifiedBy).HasColumnName("ModifiedBy");

            // Relationships        
            HasOptional(t => t.CreatedByUser)
               .WithMany(t => t.CreatedByAuditZone)
               .HasForeignKey(d => d.CreatedBy);

            HasOptional(t => t.ModifiedByUser)
                .WithMany(t => t.ModifiedByAuditZone)
                .HasForeignKey(d => d.ModifiedBy);


        }
    }

my model is defined as follows,

    public class AuditZone
    {
        public AuditZone()
        {
            AuditZoneUploadedCOESDetails = new List<UploadedCOESDetails>();
            AuditZonePostcode = new List<Postcodes>();
        }


        public int Id { get; set; }
        public string Description { get; set; }     
        public bool Valid { get; set; }       
        public DateTime CreatedDate { get; set; }
        public int? CreatedBy { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public int? ModifiedBy { get; set; }

        public virtual UserProfile CreatedByUser { get; set; }
        public virtual UserProfile ModifiedByUser { get; set; }

        public virtual ICollection<UploadedCOESDetails> AuditZoneUploadedCOESDetails { get; set; }
        public virtual ICollection<Postcodes> AuditZonePostcode { get; set; }
    }

what I need to do is someone get the key property and the table name..

at the moment I was given the following code

   TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), true).SingleOrDefault() as TableAttribute;

            // Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
            string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;

            // Get primary key value (If you have more than one key column, this will need to be adjusted)
            var keyNames = dbEntry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).ToList();

            string keyName = keyNames[0].Name;

This works if I add the data annotation [Key] and [Table] in my model. But I already have them defined in the configuration, so I am trying not to duplicate work...

any thoughts?

user2206329
  • 2,792
  • 10
  • 54
  • 81
  • The [answer here](http://stackoverflow.com/questions/7253943/entity-framework-code-first-find-primary-key) might be of help. I think you can also get the table name within that same code using `var name = set.EntitySet.ElementType.Name;` as in this [other question](http://stackoverflow.com/questions/6106842/entity-framework-get-table-name-from-the-entity). However I haven't done it myself and they are _old_ questions so there might be now a better/easier approach (or this might not even work) – Daniel J.G. Jan 15 '14 at 09:33
  • Hi did you get it working? Im in the same issue. Thanks a lot – VAAA Apr 06 '14 at 03:50

0 Answers0