Entity Framework 4 - 通过 ID 检索实体的通用方法

发布于 2024-10-27 03:53:07 字数 623 浏览 1 评论 0原文

我正在编写通过 ID 获取单个实体的方法:

public Customer GetCustomer(int i_CustomerID)
{
  return (from c in context.CustomerSet 
         where c.Id == i_CustomerID 
        select c).SingleOrDefault();            
}

public Movie GetMovie(int i_MovieID)
{
  return (from m in context.MovieSet 
         where m.Id == i_MovieID 
        select m).SingleOrDefault();
}

但是我有很多实体,并且此代码会重复自身。我想写一个这样的方法:

public T GetEntityByID<T>(int i_EntityID)
{
  return (from e in context.T_Set 
         where e.Id == i_EntityID 
        select e).SingleOrDefault();
}

有办法实现吗?

I am writing method for fetching single entities by their ID :

public Customer GetCustomer(int i_CustomerID)
{
  return (from c in context.CustomerSet 
         where c.Id == i_CustomerID 
        select c).SingleOrDefault();            
}

public Movie GetMovie(int i_MovieID)
{
  return (from m in context.MovieSet 
         where m.Id == i_MovieID 
        select m).SingleOrDefault();
}

But I have many entities and this code repeats itself. I want to write a method like this:

public T GetEntityByID<T>(int i_EntityID)
{
  return (from e in context.T_Set 
         where e.Id == i_EntityID 
        select e).SingleOrDefault();
}

Is there a way to achieve that ?

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

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

发布评论

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

评论(3

烦人精 2024-11-03 03:53:07

我实际上并没有执行这个,但它可以编译,并且可能与您想要实现的目标类似:

    public static void Testing()
    {
        SelectEntity<MyObject>(r => r.MyObjectId == 1);
    }

    public static T SelectEntity<T>(Expression<Func<T, bool>> expression) where T : EntityObject
    {
        MyContext db = new MyContext();
        return db.CreateObjectSet<T>().SingleOrDefault(expression);
    }

I haven't actually executed this but it compiles and is probably something along the lines of what you are trying to achieve:

    public static void Testing()
    {
        SelectEntity<MyObject>(r => r.MyObjectId == 1);
    }

    public static T SelectEntity<T>(Expression<Func<T, bool>> expression) where T : EntityObject
    {
        MyContext db = new MyContext();
        return db.CreateObjectSet<T>().SingleOrDefault(expression);
    }
自此以后,行同陌路 2024-11-03 03:53:07

问题是没有通用的超类型具有您所寻求的相关属性。然而,使用 EF 所使用的内置 T4 代码生成工具来代码生成您的获取方法很容易。这里有一个很好的链接,介绍如何连接并生成您需要的代码。

http://msdn.microsoft.com/en-us/data/gg558520

The problem is that there is no common super type that has the relevant properties that you seek. It is easy, however, to code generate your fetch methods using the in-built T4 code generation tool that EF is using. Here is a good link on how to hook in and generate the sort of code you need.

http://msdn.microsoft.com/en-us/data/gg558520

愚人国度 2024-11-03 03:53:07

如果您知道您的通用存储库将始终与具有相同名称和相同类型的 PK 的实体类型一起使用,您可以简单地定义如下接口:

public interface IEntity
{
    int Id { get; }
}

并在生成的实体的部分部分中实现此接口或将 T4 模板修改为自动包含它。然后,您的存储库将被定义为:

public interface IRepository<TEntity> where T : IEntity
{
    ...
}

如果 PK 的类型可以更改,但名称仍然相同,您可以将实体接口改进为:

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

并且存储库的定义将是:

public interface IRepository<TEntity, TKey> where TEntity : IEntity<TKey>
{
    ...
}

如果您想要能够与实体一起使用的通用存储库使用不同的 PK 名称和类型检查这个回答。该解决方案可能也适用于复合 PK(或进行少量修改)。

If you know that your generic repository will be always used with entity types which have PK with the same name and the same type you can simply define interface like this:

public interface IEntity
{
    int Id { get; }
}

and either implement this interface in partial part of your generated entities or modify T4 template to include it automatically. Your repository will be then defined as:

public interface IRepository<TEntity> where T : IEntity
{
    ...
}

If the type of PK can change but the name is still the same you can improve the entity interface to:

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

and the definition of repository will be:

public interface IRepository<TEntity, TKey> where TEntity : IEntity<TKey>
{
    ...
}

If you want generic repository which is able to work with entities with different PK's name and type check this answer. That solution should probably also work (or with small modification) with composite PKs.

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