有没有办法通过反射或其他方式获取实体 ID 字段的名称?

发布于 2024-07-25 21:04:42 字数 112 浏览 6 评论 0原文

我正在尝试获取实体的 ID 字段名称(属性名称),这可能吗?

用户用户=新用户(); //用户是一个实体

字符串 idField = ?????? //用户.用户Id

I am trying to get the ID field name (property name) of an entity, is it possible?

User user= new User(); //User is an Entity

string idField = ??????? //user.UserId

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喵星人汪星人 2024-08-01 21:04:42
public static IEnumerable<string> GetIdFields<TEntity>() where TEntity
  : EntityObject
{
    var ids = from p in typeof(TEntity).GetProperties()
              where (from a in p.GetCustomAttributes(false)
                     where a is EdmScalarPropertyAttribute &&
                       ((EdmScalarPropertyAttribute)a).EntityKeyProperty
                     select true).FirstOrDefault()
              select p.Name;
    return ids;
}

public static string GetIdField<TEntity>() where TEntity : EntityObject
{
    IEnumerable<string> ids = GetIdFields<TEntity>();
    string id = ids.Where(s => s.Trim().StartsWith(typeof(TEntity).Name.
                  Trim())).FirstOrDefault();
    if (string.IsNullOrEmpty(id)) id = ids.First();
    return id;
}

您可以将两个功能合并为一个或设置搜索条件。

public static IEnumerable<string> GetIdFields<TEntity>() where TEntity
  : EntityObject
{
    var ids = from p in typeof(TEntity).GetProperties()
              where (from a in p.GetCustomAttributes(false)
                     where a is EdmScalarPropertyAttribute &&
                       ((EdmScalarPropertyAttribute)a).EntityKeyProperty
                     select true).FirstOrDefault()
              select p.Name;
    return ids;
}

public static string GetIdField<TEntity>() where TEntity : EntityObject
{
    IEnumerable<string> ids = GetIdFields<TEntity>();
    string id = ids.Where(s => s.Trim().StartsWith(typeof(TEntity).Name.
                  Trim())).FirstOrDefault();
    if (string.IsNullOrEmpty(id)) id = ids.First();
    return id;
}

You could merge both funcs into one or set your search conditions.

蓝咒 2024-08-01 21:04:42

如果您可以获得实体的 EntitySet 或 EntityType,那么您可以使用 KeyMembers 属性:

public IEnumerable<string> GetIdProperties(EntitySetBase entitySet)
{
  return GetIdProperties(entitySet.ElementType);
}

public IEnumerable<string> GetIdProperties(EntityTypeBase entityType)
{
  return from keyMember in entityType.KeyMembers
         select keyMember.Name
}

您可以从上下文中获取通用对象集:

public ObjectSet<TEntity> GetEntitySet<TEntity>(ObjectContext context)
{
    return context.CreateObjectSet<TEntity>();
}

If you can get the EntitySet, or the EntityType, for the entity, then you can use the KeyMembers property:

public IEnumerable<string> GetIdProperties(EntitySetBase entitySet)
{
  return GetIdProperties(entitySet.ElementType);
}

public IEnumerable<string> GetIdProperties(EntityTypeBase entityType)
{
  return from keyMember in entityType.KeyMembers
         select keyMember.Name
}

You can obtain a generic object set from the context:

public ObjectSet<TEntity> GetEntitySet<TEntity>(ObjectContext context)
{
    return context.CreateObjectSet<TEntity>();
}
゛时过境迁 2024-08-01 21:04:42

帖子 在实体框架支持论坛中展示了如何使用反射来查找 ID 字段及其详细信息。

This post in the Entity Framework support forums shows how to use reflection to find ID fields and their details.

久随 2024-08-01 21:04:42

实体类仍然是一个继承自 System.Data.Objects.DataClasses.EntityObject 的类,因此,我认为反射仍然可以对其起作用。

你尝试过吗? 你有什么错误吗?

Entity class is still a class which inherits from System.Data.Objects.DataClasses.EntityObject, so, i think reflection will still work on it.

Have you tried it? Do you get any error?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文