Entity Framework - Querying Inheritance

I was tasked as a homework assignment to make a task tracker. I wanted to learn entity framework for this assignment, especially how to use the inheritance aspects. Projects, tasks and sub-tasks all have a lot of similar properties, so I thought I would use inheritance, but can't figure out how to query particular projects.

I drew this diagram in Visual Studio:

I then created the database from this model. How can I get an Employees Projects?

I've started with this:

ModelContainer m = new ModelContainer();
var employee = (from e in m.Employees 
               where e.UserName == username
               select e).First<Employee>();

But ((Employee)employee).Projects is not available, but ((Employee)employee).Items is. ((Employee)employee).Items.Projects is also not available. How do I get an Employee's projects? Should I add a Navigation Property to Employees for this?

Answers


You'll have to use the Queryable.OfType(TResult) extension method in order to filter the entities of type Manager:

using (var model = new ModelContainer())
{
    Manager manager = (from m in model.Employees.OfType<Manager>()
                       where m.UserName == username
                       select m).FirstOrDefault();
}

Related resources:


Need Your Help

Passing variable in a url in header function

php url variables header

I am trying to pass a variable in a URL within a header function. I know I can do it like this

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.