Friday, June 2, 2017

The type 'Whatever' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

In an Entity Framework paradigm if foo and bar are both IQueryable<Whatever> in which several tables queried off an .edmx are joined together and then aggregated to a custom type like so...

var foo  =  from f in DbContext.Fleas
from d in DbContext.Dogs.Where(d => d.DogID == f.DogID)
from m in DbContext.Men.Where(m => m.ManID == d.ManID)
where d.Name != "Lucky"
orderby f.Name, d.Name, m.Name descending
select new Whatever()
{
   FleaName = f.Name,
   DogName = d.Name,
   DogOwner = m.Name
};

...then if foo doesn't fill in the exact same fields as bar and then we ultimately jam two IQueryable<Whatever> into one with var baz = foo.Except(bar); or something, we will get this confusing error message. By the way, you want to try your hand at an Onion Architecture with and .edmx I think this is the way to do it. Map joined gunk off to your own types. If there is only one table involved and not some joining, I guess you still have to map it off to a POCO in the core project. Writing back to the database should be easier than reading. In an Onion Architecture, I guess an IQueryable has to get popped off to an IEnumerable or another collection before being handed out of the repository to a core interface implementation (the interface method signatures demand Lists or Dictionaries or whatever but not IQueryables) as not doing so would represent an external dependency bleeding in. Not cool. Sucks huh? The ORM isn't much of an ORM when it comes to abstraction. You'd have to map off a second time from what comes up off the ORM.

No comments:

Post a Comment