I'm tryng to use Table per Type approach to do a inheritance with EF Code First. My actual structure s like this:
public partial class Person
{
public int ID {get; set;}
public string Name { get; set; }
}
public partial class Employee : Person
{
public int ID { get; set; }
public string Document{ get; set; }
}
With this structure, for example, I can do this:
var obj = new Employee();
string a = obj.Name; <--- The property Name is from Person model
However, is not possible do this kind of relation with the opposite way (like was done in LINQ):
var obj2 = new Person();
string b = obj2.Person.Document; <--- It cannot resolve 'Person' symbol
Is there a way to access the model Person from a Employee object?
Thanks in advance.
As everyone has mentioned in the comments Person is not an employee and cannot access employee properties. You can use the "as" operator which will convert a person to an employee object if it is an employee, if it is not an employee it will be null.
Hopefully the following will get you in the right direction:
var obj2 = new Person();
string b;
var employee = obj2 as Employee;
if (employee != null)
{
b = obj2.Person.Document;
}
In this case Person will never be an employee because it is created right before