I am learning EF and Model First approach. I had created a test app and all works, but with strange bug. I have a table, which has inheritance.
Here it is my EDM scheme: http://gyazo.com/fbec21b5fbfc47dd76ff3eaa38f86d36.png
Also i had a three rows in my Person and Employee tables. And this is how i select Data from the employee table:
private static void GetEmployees()
{
using (EmployeesModelContainer context = new EmployeesModelContainer())
{
int rowNumber = 0;
var selectEmployeesQuery = from employee in context.Persons.OfType<Employee>()
select employee;
foreach (var employee in selectEmployeesQuery)
{
Console.WriteLine("{0}. {1} {2} ({3} лет, {4}, {5} образование) - {6}.", ++rowNumber, employee.Name, employee.Surname, employee.Age, (employee.Sex == Sex.Man) ? "Муж" : "Жен", employee.EducationLevel.Level, employee.Profession.NameOfProfession);
}
}
}
And here it is what output i have: http://gyazo.com/f2b5f9f052f2a352b6d7ef01a20904f2.png You can see that after third row i have an empty row! I broke my head from where it is adding!
If i change my SelectQuery like this all will be allright.
var selectEmployeesQuery = from employee in context.Persons
select employee;
P/S. I am not sure about my other queries, INSERT, UPDATE and DELETE. Am i right?
private static void AddNewEmployee()
{
using (EmployeesModelContainer context = new EmployeesModelContainer())
{
Employee newEmployee = new Employee
{
Name = "Вася",
Surname = "Пупкин",
Age = 34,
Sex = Sex.Man,
ProfessionId = 4,
EducationLevelId = 1
};
context.Persons.Add(newEmployee);
context.SaveChanges();
}
}
private static void UpdateEmployee()
{
using (EmployeesModelContainer context = new EmployeesModelContainer())
{
var updateEmployeeQuery = (from employee in context.Persons.OfType<Employee>()
where employee.Name == "Вася"
select employee).FirstOrDefault();
updateEmployeeQuery.Age = 39;
updateEmployeeQuery.EducationLevelId = 2;
context.SaveChanges();
}
}
private static void DeleteEmployee()
{
using (EmployeesModelContainer context = new EmployeesModelContainer())
{
var deleteEmployeeQuery = (from employee in context.Persons.OfType<Employee>()
where employee.Name == "Вася"
select employee).FirstOrDefault();
context.Persons.Remove(deleteEmployeeQuery);
context.SaveChanges();
}
}
The question is, If I need to add or delete some data in Employees table, why i am adding or deleting in Person table and it is allright? Data adding in Person and in Employees tables. And if I Updating some data in Employees table, why the data is updating in Person table correctly?