1

I wanted to start using Entity Framework for my projects. A new project which we'll be starting soon, will have an Employee table. I was initially planning to have a IEmployee Interface, which will be implemented by a Manager and Staff classes, which will allow different functionality but will both store data in the Employee table, with a flag in the table distinguishing them.

If I use DB First, and design my Employee table and then use Entity Framework, i know the .tt file will have a partial class Employee. I could then make my own Manager and Staff classes which implement the partial class Employee. But then how would I store that back in the db using Entity Framework? Could I just do something like

// currentManager would be the manager object
dbContext.Emplyee.Add(currentManager);
dbContext.SaveChanges();

would entity framework be ok with this, even though i'm passing a Manager object into Employee to save? Or is there a better way to do this? And same with retrieving, how could I use Entity Framework to get back a Manager or Staff? Would I need to get a Employee back first and then cast it? Something like

var employee = from employees... // get employee
Manager currentManager = (Manager)employee;

1 Answer 1

2

Yes, you can use EF DB first and have multiple classes derived from a common base class that are stored in the same database table.

The "flag" you envision is called a discriminator by EF. It is a column in the table that specifies which subtype the record is an instance of.

With database first, you need to tweak the model generated by EF to get this working, but it's fairly straightforward. I think the easiest way to set this up is to

  1. Create the database model in SQL, being sure the Employee table has a NOT NULL property like EmployeeType that can be used as the discriminator. I think this can be any type, but an int will work fine.
  2. Create your EF model ( Add | New Item... | Data | ADO.NET Entity Data Model ), and map the Employee table (and anything else you need).
  3. Double click the generated .edmx file to open the entity framework designer.
  4. Right mouse click on the canvass and select Add New... | Entity and create a Manager entity that derives from Employee. Repeat for Staff. Your designer should look like this:Entities
  5. Right mouse click on Manager and select Table Mapping. In the mapping details, specify that it maps to Employee and add a condition that is When EmployeeType = 1. Do the same for Staff, but make the mapping When EmployeeType = 2.screenshot of mapping details
  6. Last, you need to delete the EmployeeType property from the Employee mapping.

That being done, you can extend the Manager and Staff classes (that will now be generated by EF as partial classes) to have whatever business logic you want, and do queries/etc with EF via the Employees mapping:

public class Manager : Employee
{
    public void customManagerMethod() { }
}

public class Staff : Employee
{
    public void customStaffMethod() { }
}

class Program
{
    static void Main(string[] args)
    {
        using (var db = new dbfirstEntities())
        {
            Manager m = new Manager
            {
                FirstName = "Joe",
                LastName = "Bigshot"
            };

            Staff s = new Staff
            {
                FirstName = "Joe",
                LastName = "Schmoe"
            };

            db.Employees.Add(m);
            db.Employees.Add(s);

            db.SaveChanges();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.