Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c# - How to get list of database table primary key columns in EF Core

ASP.NET Core MVC application using EF Core. In Linq to SQL, this code returns list of database table primary key columns names:

    /// <summary>
    /// Database primary key names
    /// </summary>
    public IList<string> DatabasePrimaryKey(Type dbContextPocoType)
    {
        List<string> pk = new List<string>();

        foreach (PropertyInfo p in dbContextPocoType.GetProperties())
        {
            var ca = p.GetCustomAttributes(typeof(ColumnAttribute), true);

            if (ca.Length == 0) continue;
            var atr = (ColumnAttribute)ca.Single();

            if (!atr.IsPrimaryKey) continue;
            pk.Add(atr.Name);
        }

        return pk;
    }

In EF Core I tried

var entry = ctx.Entry(dbContextPocoType);
var primaryKey = entry.Metadata.FindPrimaryKey();

IList<string>  keys = primaryKey.Properties.Select(x => x.Name).ToList();
return keys;

But this returns C# property names - how to get database table column names in EF Core?

Update: using answer I created this method:

    public IList<string> DatabasePrimaryKey<TPoco>()
    {
        var entry = ctx.Entry(typeof(TPoco));
        var primaryKey = entry.Metadata.FindPrimaryKey();
        var entityType = ctx.Model.FindEntityType(typeof(TPoco).Name);
        var schema = entityType.GetSchema();
        var tableName = entityType.GetTableName();
        IList<string> keys = primaryKey.Properties
             .Select(x => x.GetColumnName(StoreObjectIdentifier.Table(tableName, schema)))
             .ToList();
        return keys;
    }

Can this method be improved?

question from:https://stackoverflow.com/questions/65837188/how-to-get-list-of-database-table-primary-key-columns-in-ef-core

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In EF Core 3.x You could try the IProperty extension method GetColumnName instead.

var entry = ctx.Entry(dbContextPocoType);
var primaryKey = entry.Metadata.FindPrimaryKey();
var primaryKeyColumns = primaryKey.Properties
                     .Select(property => property.GetColumnName())
                     .ToList()

return primaryKeyColumns;

For EF Core 5, you need to use the overload that accepts a StoreObjectIdentifier: GetColumnName(IProperty, StoreObjectIdentifier).

Update for EF 5:

public IList<string> DatabasePrimaryKey<TPoco>()
{
    var entityType = ctx.Model.FindEntityType(typeof(TPoco));
    var primaryKey = entityType.FindPrimaryKey();
    var schema = entityType.GetSchema();
    var tableName = entityType.GetTableName();
    var storeObjectIdentifier = StoreObjectIdentifier.Table(tableName, schema);
    IList<string> primaryKeyColumns = primaryKey.Properties
        .Select(x => x.GetColumnName(storeObjectIdentifier))
        .ToList();
    return primaryKeyColumns;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...