asp.net mvc - Unable To Cast a Partial Class based on LINQ-2-SQL Object -


Say I have a database table that I drag over to drag on my Linq-2-SQL DBML file.

This is called me an animal.

Then I am creating a square to expand it like:

  public partial class animal {/ my extra code here}   

But I also want to do this, there is a class like this:

  public class zebra: animal {// even extra additional code }   

The problem is that I receive an invalidcast exception from my repository, when I do this:

  animal a = dataContext.Animals.Where (c = & Gt; c.id.Equals (ID)) SingleOrDefault () .; Return (Zebra) A;   

I have snatched my classes in the place where it is completely empty in an effort to work, so it does not work. no benefit.

This casting process was used to work in a non-LINQ-2-SQL project, which is switching to MVC / LINQ.

Too many Googling stumped completely on this ...

Assume this can not be done, what is the best alternative approach? Interface?

Jake

This does not work because a zebra is a beast, not all animals Zebras are when you get the class, then it does not know anything about the extra methods and properties of the class. If you can put it in the form of zebra, it will not remember them.

What you probably should do is create a constructor on zebra which takes an animal and produces zebra objects from that animal (if possible). / P> Return DataConnectExamples. Where (c => c.id == id). Select (a = & gt; New Zebra (A)) .SingleOrDefault ();

Alternatively, you can see column discriminators and subclassing in your data reference using Pak.

Comments