Tuesday, August 05, 2008

Class member Entity.Id is unmapped.

ok, the problem come from the fact that we have a number of tables (linq entities), but only a few of them have the Id column, and only on those few i need to run a certain method.
so how to do this?

i created the generic method


MMethod(int id) where T : Entity, IEntity
{
var r = from s in source.GetEntity()
where s.Id == id
select s;
}



and here i started getting the errors...
first of course i could not use s.Id because the general T doesn't have Id member defined.
so i started looking where to define it - and first defined it in the interface.
now, the interface would have the property Id and some of the linq entities would also have it, but on evaluation of the var r i would get

The mapping of interface member [...] is not supported


nice...
some reading on google and i started looking for another place to add this member.
the other obvious choice - the entity class.
this seemed to be my last chance, after i tried coming up with an extension method or whatever desperate solution i could think of.

so, member added, all compiled nicely, but while in debug i decided to see what the var r evaluates to and saw a sweet message:

Class member Id is unmapped


so...WTF? Id is now nicely defined and visible everywhere, so how come this error? and what does it mean??

some browsing in google, and i come to this page

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2446465&SiteID=1


at first quick look i couldn't find any solution, but, ooo how nice that i didn't close the page immediately, because after some time i went back to it, as something stuck as a neat idea - the guy was talking about a possible bug in linq implementation, but gave a few examples of when a statement worked and when it didn't, so i decided to give a try to one of the statements that he claimed to have worked and....voila!
problem resolved.
while waiting for the build i came upon another page that confirmed this solution here: http://stuff.hornbostel.com/?cat=10

neat
thanks guys