实体框架简单通用GetByID但具有不同的PK名称 [英] Entity Framework Simple Generic GetByID but has differents PK Name

查看:146
本文介绍了实体框架简单通用GetByID但具有不同的PK名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些身体可以帮助我创建一个通用的GetByID方法,
这里的挑战是,我有很多实体,每个都有不同的PK名称。



我看到有一个通用GetByID的几个例子,但是其中许多具有与(id)相同的PK名称。



谢谢。

解决方案

这里是使用单个属性键的实体的存储库构建基础存储库类的示例。 GetByKey 方法独立于密钥名称或类型。

  using System ; 
使用System.Data;
使用System.Data.Metadata.Edm;
使用System.Data.Objects;
使用System.Linq;

命名空间EntityKeyTest
{
//具有简单键的实体的基本存储库类
public abstract class RepositoryBase< TEntity,TKey>其中TEntity:class
{
private readonly string _entitySetName;
私有readonly字符串_keyName;

protected ObjectContext Context {get;私人集合}
protected ObjectSet< TEntity> ObjectSet {get;私人集合

protected RepositoryBase(ObjectContext context)
{
if(context == null)
{
throw new ArgumentNullException(context);
}

上下文=上下文;
ObjectSet = context.CreateObjectSet< TEntity>();

//获取当前实体类型的实体集
var entitySet = ObjectSet.EntitySet;
//为当前实体类型创建实体集的全名
_entitySetName = context.DefaultContainerName +。 + entitySet.Name;
//获取实体的键属性的名称
_keyName = entitySet.ElementType.KeyMembers.Single()。Name;


$ b public virtual TEntity GetByKey(TKey key)
{
//构建实体键
var entityKey = new EntityKey(_entitySetName,_keyName,key );
//查询第一个当前状态管理器,如果实体未找到查询数据库!
return(TEntity)Context.GetObjectByKey(entityKey);
}

//其他存储库实现
}
}

使用此代码时有一个特技。如果不使用从 ObjectContext 派生的生成类,并且直接使用 ObjectContext ,则必须手动设置 DefaultContainerName 您的模型使用。



编辑:



此方法适用于经典EF。如果需要,我可以考虑Code-first的版本。


Some body can help me to create a generic GetByID method, The challenge here, is that I Have many entities, each of them has a different PK Name.

I See several example with a Generic GetByID, but many of them has the same PK Name like (id).

Thanks.

解决方案

Here is example of base repository class for repositories build for entity with single property key. The GetByKey method is independent on key name or type.

using System;
using System.Data;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Linq;

namespace EntityKeyTest
{
    // Base repository class for entity with simple key
    public abstract class RepositoryBase<TEntity, TKey> where TEntity : class
    {
        private readonly string _entitySetName;
        private readonly string _keyName;

        protected ObjectContext Context { get; private set; }
        protected ObjectSet<TEntity> ObjectSet { get; private set; }

        protected RepositoryBase(ObjectContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            Context = context;
            ObjectSet = context.CreateObjectSet<TEntity>();

            // Get entity set for current entity type
            var entitySet = ObjectSet.EntitySet;
            // Build full name of entity set for current entity type
            _entitySetName = context.DefaultContainerName + "." + entitySet.Name;
            // Get name of the entity's key property
            _keyName = entitySet.ElementType.KeyMembers.Single().Name;
        }

        public virtual TEntity GetByKey(TKey key)
        {
            // Build entity key
            var entityKey = new EntityKey(_entitySetName, _keyName, key);
            // Query first current state manager and if entity is not found query database!!!
            return (TEntity)Context.GetObjectByKey(entityKey);
        }

        // Rest of repository implementation
    }
}

There is one trickler when using this code. If you don't use generated class derived from ObjectContext and you use ObjectContext directly you must manually set DefaultContainerName used by your model.

Edit:

This method is for classic EF. I can think about version for Code-first if needed.

这篇关于实体框架简单通用GetByID但具有不同的PK名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆