开发者

Exception on LINQ query against LINQ2SQL/WCF class that inherits from an abstract class

开发者 https://www.devze.com 2023-03-14 04:15 出处:网络
I\'m having some problems running a LINQ query off of a LINQ2SQL class that inherits from a base abstract class.

I'm having some problems running a LINQ query off of a LINQ2SQL class that inherits from a base abstract class.

[DataContract]
public abstract class AbstractClass
{
    [DataMember]
    public abstract int Foo { get; set; }
}


// LINQ2SQL class.
[DataContract]
public class ConcreteClass : AbstractClass
{
    [DataMember]
    public override int Foo { ... }
}

The problem occurs when I try to run a LINQ query against my Concrete Class. I get a SystemExce开发者_如何学Goption saying, "Class member AbstractClass.Foo is unmapped." The query would look something like the following:

// Sample LINQ query.
result = from c in dataContext.ConcreteClasses where c.Foo == 42 select c;

Notice how the query is against CocnreteClass, but the error is on the AbstractClass. Any ideas whats going on here?


Does your abstract class have a constructor in it? I think I have had something similar to this before, and adding a protected constructor to the abstract class seemed to have fixed it.


Are your abstract and concrete classes LINQ2SQL classes using Table-Per-Type inheritance or some other data-centric inheritance? I know that when you pass EF classes over WCF they lose inheritance information for some reason.


It looks like the compiler was having problems infering the correct type. Instead of using the class returned by the LINQ2SQL context, it was using the base class, so I had to cast it to the correct type explicitly.

This previous example does NOT work:

result = from c in dataContext.ConcreteClasses where c.Foo == 42 select c;

This example does work:

result = from c in dataContext.ConcreteClasses where ((ConcreteClass)c).Foo == 42 select c;

Odd, but it works. Upvotes to anyone who can figure out why it would do that.

(Reference: Very strange inheritance behavior.)

0

上一篇:

:下一篇

精彩评论

暂无评论...
验证码 换一张
取 消