I am into a situation that my table names are different from the class property on the model using mapping in EF 6. the Model and Database goes like this:
public class AGENTMap : EntityTypeConfiguration<AGENT>
{
public AGENTMap()
{
// Primary Key
this.HasKey(t => new {t.AgentCode });
// Properties
this.Property(t => t.AgentCode)
.HasMaxLength(10);
this.Property(t => t.AgentName)
.HasMaxLength(30);
// Table & Column Mappings
this.ToTable("AGENT");
this.Property(t => t.agent_cd).HasColumnName("agent_cd");
this.Property(t => t.agent_nm).HasColumnName("agent_nm");
}
}
which is equivalent to an AGENT class that has those properties. The problem is when i Try to get the primary Key using this code snippet:
ObjectContext objectContext = ((IObjectContextAdapter)_context).ObjectContext;
ObjectSet<TEntity> objSet = objectContext.CreateObjectSet<TEntity>();
IEnumerable<string> keyNames = objSet.EntitySet.ElementType.KeyMembers
.Where(p => p.MetadataProperties.Any(m => m.PropertyKind == PropertyKind.Extended
&& Convert.ToString(m.Value) == "Identity"))
.Select(e => e.Name).ToList();
return keyNames;
it returns the Property Name on the Mapping class. I want to get the "agent_cd"..which is the db column name.. Is there a way in EF6 to get the exact Column Name on the Db??