简体   繁体   English

使用泛型类时如何通过PK查询EF?

[英]How to query EF through the PK when using a generic class?

I'm trying to implement a generic class that will interact with a generic repository, and all is fine except for when I have to deal with getting objects out of the repository. 我正在尝试实现一个将与通用存储库进行交互的通用类,一切都很好,除了必须处理将对象从存储库中取出时。

I'm going to have a virtual method in the generic class which will receive an int and I want to use that int to form a query to the repository that gets objects by their primary key. 我将在泛型类中有一个虚拟方法,该方法将接收一个int并且我想使用该int来构成对通过主键获取对象的存储库的查询。 I have a feeling I need to work with the EntityKey property in EF, but not too sure how. 我觉得我需要使用EF中的EntityKey属性,但不太确定如何使用。

Anyway, here's what I'm trying to do in code, I hope someone will have suggestions on how to accomplish what I want: 无论如何,这是我要在代码中尝试做的事情,希望有人对如何完成我想要的事情有建议:

public virtual T Get(int PrimaryKey) {
    this.Repository.Select(
        t =>
            (t.PRIMARYKEY == PrimaryKey)).Single();
}

I want to extend this class with more specialized classes, but since most of them only get their objects by querying the PK, it makes since to me to have a base method that can do it. 我想用更专业的类来扩展该类,但是由于它们中的大多数只能通过查询PK来获取其对象,因此对我而言,这使得它具有可以执行此操作的基本方法。

Thanks in advance for any suggestions! 在此先感谢您的任何建议!

UPDATE 更新

So, here's where I've gotten with reflection, and I doubt its the proper way, but it somewhat works... I'm getting a NotSupportedException with the message LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression. 因此,这是我进行反思的地方,我怀疑它的正确方法,但是它有些起作用...我收到一个NotSupportedException消息,消息为LINQ to Entities无法识别方法'System.Object GetValue(System .Object,System.Object [])方法,并且该方法无法转换为商店表达式。 . Although I understand what it says and why it's saying, I'm not sure how to overcome it when my code looks like this: 尽管我了解它的意思和原因,但是我不确定如何在我的代码看起来像这样时克服它:

private readonly string TEntityName = typeof(T).Name;

public virtual T Get(
    int PrimaryKey) {
    return this.Repository.Select(
        t =>
            (((int)t.GetType().GetProperties().Single(
                p =>
                    (p.Name == (this.TEntityName + "Id"))).GetValue(t, null)) == PrimaryKey)).Single();
}

Hoping that someone who knows how to use reflection, unlike me, can point me in the right direction. 希望与我不同的是,知道如何使用反射的人可以为我指明正确的方向。 Thanks! 谢谢!

Retrieving an entity by a PK using EF requires an expression/predicate, like this: 使用EF通过PK检索实体需要一个表达式/谓词,如下所示:

Expression<Func<Order,bool>> predicate = x => x.OrderId == 1;
return ctx.Orders.Single(predicate);

There is no easy way (short of reflection or expression tree creation) to be able to dynamically create this predicate. 没有简单的方法(缺少反射或创建表达式树)来动态创建此谓词。

What you could do is accept the predicate as a parameter: 您可以做的就是接受谓词作为参数:

public virtual T Get(Expression<Func<T,bool>> predicate) {
    this.Repository.Select(predicate).Single();
}

Also make sure you put some generic constraints on T (either at the class/method level). 还要确保对T施加一些通用约束(在类/方法级别上)。

http://msdn.microsoft.com/en-us/library/bb738961.aspx is way to go. http://msdn.microsoft.com/zh-cn/library/bb738961.aspx可行的方法。 Then use http://msdn.microsoft.com/en-us/library/bb738607.aspx , spend some time in debugger and your misson is completed. 然后使用http://msdn.microsoft.com/zh-cn/library/bb738607.aspx ,花一些时间在调试器上,您的misson操作就完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM