4

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??

4

1 回答 1

4

Rowan Miller写了另一篇博文,准确展示了如何在 EF 6 中获取列名。

public static string GetColumnName(Type type, string propertyName, DbContext context)
{
    var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;

    // Get the part of the model that contains info about the actual CLR types
    var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

    // Get the entity type from the model that maps to the CLR type
    var entityType = metadata
            .GetItems<EntityType>(DataSpace.OSpace)
            .Single(e => objectItemCollection.GetClrType(e) == type);

    // Get the entity set that uses this entity type
    var entitySet = metadata
        .GetItems<EntityContainer>(DataSpace.CSpace)
        .Single()
        .EntitySets
        .Single(s => s.ElementType.Name == entityType.Name);

    // Find the mapping between conceptual and storage model for this entity set
    var mapping = metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpace)
            .Single()
            .EntitySetMappings
            .Single(s => s.EntitySet == entitySet);

    // Find the storage entity set (table) that the entity is mapped
    var tableEntitySet = mapping
        .EntityTypeMappings.Single()
        .Fragments.Single()
        .StoreEntitySet;

    // Return the table name from the storage entity set
    var tableName = tableEntitySet.MetadataProperties["Table"].Value ?? tableEntitySet.Name;

    // Find the storage property (column) that the property is mapped
    var columnName = mapping
        .EntityTypeMappings.Single()
        .Fragments.Single()
        .PropertyMappings
        .OfType<ScalarPropertyMapping>()
        .Single(m => m.Property.Name == propertyName)
        .Column
        .Name;

    return tableName + "." + columnName;
}
于 2016-09-26T18:27:36.723 回答