Frage

I've been trying to get started using entity framework and have ran across a peculiar behavior. I've prepared this short example to demonstrate.

In short, I'm trying to create a table-per-type model, using a base type and an inherited type, "Person" and "Employee". The problem is, when I query the database using a line such as

var list = entities.PersonSet.OfType<Person>().ToList();

I expect to get a list of person objects. Although the list is of type List, all the objects in the list are Employee objects, even though only one of the records in the table is for an employee. It looks like EF is returning the wrong type.

However, add another inherited type from Person, such as "Superhero" (I'm trying to make this an obvious example :), and create a Superhero record, and now the same command returns a mixture of Persons, Employees and Superheroes as expected (where previously I only got Employees, even though they weren't Employees)

Details

I'm using VS2010 and an Oracle database, with ODAC 11.2.0.3 ! I need some help to confirm whether this is an Oracle related issue, or if this also happens with MSSQL database and DDL generation.

I create a new console application, and go to Add->New file->ADO.NET Entity Data Model. I then draw up this model

EF Model

My code looks like this:

class Program
{
    static void Main(string[] args)
    {
        var e = new Entities();

        var result1 = e.PersonSet.OfType<Superhero>().ToList();
        var result2 = e.PersonSet.OfType<Employee>().ToList();
        var result3 = e.PersonSet.OfType<Person>().ToList();
    }
}

and the results:

result 1

All is well.

Now, if I delete the Employee entity and comment out the "var result2..." line, I get this result:

result 2

I have made no changes to the data in the database. Notice how ALL entities now have a type of Superhero! And it's worth noting that I have to set the "Superpower" property to Nullable=True, otherwise I get an exception, since only one of the records actually is a superhero (the other records don't have any entires in the PersonSet_Superhero table)

So, is this a bug in Entity Framework, ODAC or anything else, or am I doing something wrong here??

War es hilfreich?

Lösung

If you were using Oracle Express as your database that seems to be the problem. Take a look at this thread.. I went through the same thing and the culprit turned out to be Oracle XE but this works on the Oracle 11g Enterprise version.

Andere Tipps

I found that if I made the base type abstract it works in EF 5 w/ Oracle 11g xe.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top