通用存储库EF4 CTP5 getById [英] generic repository EF4 CTP5 getById

查看:123
本文介绍了通用存储库EF4 CTP5 getById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用存储库,我试图添加一个GetById方法,如下所示
C#LINQ to SQL:重构此通用GetByID方法



问题是我的存储库不使用System.Data.Linq.DataContext
代替我使用System.Data.Entity.DbContext



所以我收到错误,我试图使用

  Mapping.GetMetaType 

  return _set.Where(whereExpression).Single(); 

如何在CTP5中实现通用GetById方法?我应该在我的存储库中使用System.Data.Entity.DbContext。



这是我的存储库类的开始

  public class BaseRepository< T>其中T:class 
{

private DbContext _context;
private readonly DbSet< T> _组;

public BaseRepository()
{
_context = new MyDBContext();
_set = _context.Set< T>();

}


解决方案

基本方法只是简单的

  public T GetById(params object [] keys)
{
_set.Find (键);
}

如果你知道所有的实体都有主键叫做Id(它不是'必须在DB中调用Id,但必须映射到定义类型的属性Id),您可以简单地使用它:

 公共接口IEntity 
{
int Id {get; }
}

public class BaseRepository< T>其中T:class,IEntity
{
...

public T GetById(int id)
{
_set.Find(id);
}
}

如果数据类型不总是相同的,可以使用:

  public interface IEntity< TKey> 
{
TKey Id {get; }
}

public class BaseRepository< TEntity,TKey>其中TEntity:class,IEntity< TKey>
{
...

public TEntity GetById(TKey id)
{
_set.Find(id);
}
}

您也可以简单地使用:

  public class BaseRepository< TEntity,TKey>其中TEntity:class 
{
...

public TEntity GetById(TKey id)
{
_set.Find(id);
}
}


I have a generic repository an I am trying to add a GetById method as shown here C# LINQ to SQL: Refactoring this Generic GetByID method

The problem is my repository does not use System.Data.Linq.DataContext instead I use System.Data.Entity.DbContext

So I get errors where I try to use

Mapping.GetMetaType

and

return _set.Where( whereExpression).Single();

How can I implement a generic GetById method in CTP5? Should I be using System.Data.Entity.DbContext in my Repository.

Here is the start of my repository class

  public class BaseRepository<T> where T : class
    {

        private DbContext _context;
        private readonly DbSet<T> _set;

        public BaseRepository()
        {
            _context = new MyDBContext();
            _set = _context.Set<T>();

        }

解决方案

The most basic approach is simply

public T GetById(params object[] keys)
{
  _set.Find(keys);
}

If you know that all your entities have primary key called Id (it doesn't have to be called Id in DB but it must be mapped to property Id) of defined type you can use simply this:

public interface IEntity
{
  int Id { get; }
}

public class BaseRepository<T> where T : class, IEntity
{
  ...

  public T GetById(int id)
  {
    _set.Find(id);
  }
}

If data type is not always the same you can use:

public interface IEntity<TKey>
{
  TKey Id { get; }
}

public class BaseRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>
{
  ...

  public TEntity GetById(TKey id)
  {
    _set.Find(id);
  }
}

You can also simply use:

public class BaseRepository<TEntity, TKey> where TEntity : class
{
  ...

  public TEntity GetById(TKey id)
  {
    _set.Find(id);
  }
}

这篇关于通用存储库EF4 CTP5 getById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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