简体   繁体   English

实体框架联合和投影失败

[英]Entity Framework Union and Projection fail

This is a multi-progned question so please bear with me! 这是一个多问题的问题,请耐心等待! I have two queries: 我有两个查询:

var dynamicResult = Repository.Select<Table1>()
                        .Where(b => b.InactivatedD == null)
                        .Select(b => b.Table2);

var staticResult = Repository.Select<Table2>()
                       .Where(b => b.column == "CONSTANT");

return dynamicResult.Union(staticResult).ToList();

This works fine. 这很好。 Now I have added an additional property to the the Table2 class and have instructed EF to ignore the field within my configuration like so: 现在,我向Table2类添加了一个附加属性,并指示EF忽略配置中的字段,如下所示:

Ignore(e => NewColumn);

This too is working well as I can set the field properly without EF throwing an exception. 这也很好用,因为我可以正确设置字段,而不会引发EF异常。 Now I don't know if there is an easy way to do what I want. 现在,我不知道是否有一种简单的方法可以执行我想要的操作。 In my first query Table1 has a column that I want to use to hydrate this new column on Table2 but I don't know of an easy way to do this. 在我的第一个查询中,Table1有一个列,我想用来在Table2上合并这个新列,但是我不知道这样做的简单方法。 The only thing I have been able to come up with is: 我唯一能想到的是:

var dynamicResult = Repository.Select<Table1>()
                        .Where(b => b.InactivatedD == null)
                        .Select(b => new Table2 { Column1 = b.Table2.Column1, NewColumn = b.SomeColumn ... <additional initialization> });

This is a little messy and the initialization would get pretty long since this entity has about 15 columns I'd need to hydrate. 这有点混乱,并且初始化将花费很长时间,因为该实体有大约15列需要水合。 I could, of course, just traverse the association between Table2 and Table1 in my property rather than trying to set it in the above query but that seems like additional work and one more query to maintain. 当然,我可以在属性中遍历Table2和Table1之间的关联,而不是尝试在上述查询中进行设置,但这似乎是一项额外的工作,并且需要维护另一个查询。 Additionally, when using the method above, my union no longer works. 此外,使用上述方法时,我的联合不再起作用。 If my queries look like this: 如果我的查询如下所示:

var dynamicResult = Repository.Select<Table1>()
                        .Where(b => b.InactivatedD == null)
                        .Select(b => new Table2 { Column1 = b.Table2.Column1, NewColumn = b.SomeColumn })

var staticResult = Repository.Select<Table2>()
                       .Where(b => b.column == "CONSTANT")
                       .Select(b => new Table2 { Column1 = b.Table2.Column1, NewColumn = b.SomeColumn })

return dynamicResult.Union(staticResult).ToList();

I get an exception that the Entity or Complex type, Table 2, can not be constructed by an Entity Framework query which kind of has me at a loss. 我得到一个例外,即实体或复杂类型(表2)不能由某种让我不知所措的实体框架查询构造。 I understood why I was getting this error before I told EF to ignore the NewColumn but now I am not sure why this error is popping up. 在告诉EF忽略NewColumn之前,我理解了为什么收到此错误,但是现在我不确定为什么会弹出此错误。

In summation: is there a better way to hydrate my new column then what I have proposed above and can anyone identify why I am unable to union entities created using new from within a query? 总而言之:有没有比我上面建议的更好的方法来充实我的新列,并且谁能确定为什么我无法在查询中合并使用new创建的实体?

Thanks! 谢谢!

It is never allowed to create an entity type in an EF query irrespective of which properties you address. 无论您要解决的是哪个属性,都决不允许在EF查询中创建实体类型。 The reason is that EF has no way to track such entities, because it did not materialize them itself. 原因是EF无法跟踪此类实体,因为它本身并未实现。

You should define a type that looks like Table2 , including the new column and project to that type in both queries. 您应该定义一个类似于Table2的类型,包括新列,并在两个查询中都对该类型进行投影。 You will be able to union the queries. 您将能够合并查询。

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

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