首页 > 解决方案 > EF6 System.Data.Entity.Core.EntityKey 在 Entity Framework Core 中的等价物是什么?

问题描述

我在使用 EF 6 的 .net 框架中有以下代码。

PropertyInfo[] propInfo = myObject.GetType().GetProperties(); //myObject is of type T
foreach (PropertyInfo col in propInfo) {
    if (col.PropertyType != typeof(System.Data.Entity.Core.EntityKey))
           //do something
}

现在我正在迁移到 .Net 5 和 EF Core,如何获得等效的 .Net 5 和 EF Core typeof(EntityKey)

标签: c#entity-framework-coreentity-framework-6.net-5

解决方案


您可以使用以下 API 实现此目的:

var et = ctx.Model.FindEntityType(typeof(T));
var properties = et.GetProperties();

foreach (var property in properties)
{
    if (!property.IsKey())
    {
         // do something
    }
}

推荐阅读