如何在 EF4 中获取实体的第一个 EntityKey 名称

发布于 2024-09-11 17:23:23 字数 481 浏览 2 评论 0原文

我如何获取 Entity Framework 4 实体的第一个 EntityKey 名称,因为我正在构建一个存储库系统,并且我想通过 Id 获取一个项目(EF 中的主键是该实体的第一个实体键)

我'我使用此代码

public virtual TEntity GetById(string keyName, Guid entityId)
        {
            var entityKey = new EntityKey(this.QualifiedEntitySetName, keyName, entityId);
            object entity;
            return this.Context.TryGetObjectByKey(entityKey, out entity) ? entity as TEntity : null;
        }

我想动态获取实体键名称。 谁能帮我解决这个问题吗?

How can I get the 1st EntityKey name for an Entity for Entity Framework 4 because I'm building a repository system and I wanted to get an item by Id (which is the primary key oin EF is the 1st entitykey for the entity)

I'm using this code

public virtual TEntity GetById(string keyName, Guid entityId)
        {
            var entityKey = new EntityKey(this.QualifiedEntitySetName, keyName, entityId);
            object entity;
            return this.Context.TryGetObjectByKey(entityKey, out entity) ? entity as TEntity : null;
        }

I want to get the entity key name dynamic.
Can anyone help me with this issue?

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

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

发布评论

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

评论(4

能怎样 2024-09-18 17:23:23

var keyName = this.Context
.MetadataWorkspace
.GetEntityContainer(this.Context.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets
.First(meta => meta.ElementType.Name == this.entityName)
.ElementType
.主要成员
.Select(k => k.名称)
.FirstOrDefault();

我知道它看起来太多了,但我想通过实体名称来获取它。

var keyName = this.Context
.MetadataWorkspace
.GetEntityContainer(this.Context.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets
.First(meta => meta.ElementType.Name == this.entityName)
.ElementType
.KeyMembers
.Select(k => k.Name)
.FirstOrDefault();

I know it looks too much but u I wanted to get it by having the Entity Name.

吻风 2024-09-18 17:23:23

您可以使用以下代码从 MetaDataWorkspace 获取 Entity Key 成员集合:

ReadOnlyCollection keyMembers = db.MetadataWorkspace.GetType("", "Entity_Namespace", System.Data.Metadata.Edm.DataSpace.CSpace).MetadataProperties["KeyMembers"].Value as ReadOnlyCollection;

You can get the Entity Key members collection from MetaDataWorkspace using the following code:

ReadOnlyCollection keyMembers = db.MetadataWorkspace.GetType("", "Entity_Namespace", System.Data.Metadata.Edm.DataSpace.CSpace).MetadataProperties["KeyMembers"].Value as ReadOnlyCollection;

傲鸠 2024-09-18 17:23:23

试试这个:

/// <summary>
/// Gets the entity key for the POCO Entity type.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
private EntityKey GetEntityKey(TEntity entity)
{
    ReadOnlyMetadataCollection<EdmMember> keyMembers =
        this.ObjectSet.EntitySet.ElementType.KeyMembers;

    var entityKeyMembers = new List<EntityKeyMember>();

    //Construct the entity key for the POCO Entity type object.
    foreach (EdmMember keyMember in keyMembers)
    {
        object keyMemberValue = entity.GetType().GetProperty(keyMember.Name).GetValue(entity, null);
        entityKeyMembers.Add(new EntityKeyMember(keyMember.Name, keyMemberValue));
    }

    //Create the Entity key for our POCO Entity type object.
    return new EntityKey(this.ObjectSource.DefaultContainerName
        + "." + this.ObjectSet.EntitySet.Name, entityKeyMembers);
}

objectsource 是 objectcontext。

Try this:

/// <summary>
/// Gets the entity key for the POCO Entity type.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
private EntityKey GetEntityKey(TEntity entity)
{
    ReadOnlyMetadataCollection<EdmMember> keyMembers =
        this.ObjectSet.EntitySet.ElementType.KeyMembers;

    var entityKeyMembers = new List<EntityKeyMember>();

    //Construct the entity key for the POCO Entity type object.
    foreach (EdmMember keyMember in keyMembers)
    {
        object keyMemberValue = entity.GetType().GetProperty(keyMember.Name).GetValue(entity, null);
        entityKeyMembers.Add(new EntityKeyMember(keyMember.Name, keyMemberValue));
    }

    //Create the Entity key for our POCO Entity type object.
    return new EntityKey(this.ObjectSource.DefaultContainerName
        + "." + this.ObjectSet.EntitySet.Name, entityKeyMembers);
}

objectsource is objectcontext.

凉城已无爱 2024-09-18 17:23:23
public static string GetPrimaryKeyName(this object entity)
   {
       var prop = entity.GetType().GetProperties().Where(x =>x.GetCustomAttributes(false).Where(y => y is ColumnAttribute&&((ColumnAttribute)y).IsPrimaryKey == true).Any()).FirstOrDefault();
          if (prop != null)
              return prop.Name;
          return string.Empty;

   }
public static string GetPrimaryKeyName(this object entity)
   {
       var prop = entity.GetType().GetProperties().Where(x =>x.GetCustomAttributes(false).Where(y => y is ColumnAttribute&&((ColumnAttribute)y).IsPrimaryKey == true).Any()).FirstOrDefault();
          if (prop != null)
              return prop.Name;
          return string.Empty;

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