简体   繁体   中英

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:

Ignore(e => NewColumn);

This too is working well as I can set the field properly without EF throwing an exception. 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. 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. 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. 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. 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.

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?

Thanks!

It is never allowed to create an entity type in an EF query irrespective of which properties you address. The reason is that EF has no way to track such entities, because it did not materialize them itself.

You should define a type that looks like Table2 , including the new column and project to that type in both queries. You will be able to union the queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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