Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom method to auto-generated class in Entity Framework?

I have a class with 2 methods as follow:

public class WorkManagement
{
    public string DoYourWork(Manager manager)
    {
        //
    }

    public string DoYourWork(Employee employee)
    {
        //
    }
}

Manager and Employee are classes generated from database (in Entity Framework). I think it's ugly, for instance, when I need to extend more class, so I want to refactor this into:

 public interface IDoWork
 {
    string DoSomeWork();
 }

public class Manager:IDoWork
{
    public string DoSomeWork()
    {
        //
    }
}

public class Employee:IDoWork
{
    public string DoSomeWork()
    {
        //
    }
}

But how I can deal with auto-generated classes? How I add these thing?

Thank you.

like image 282
Quan Mai Avatar asked May 18 '11 02:05

Quan Mai


1 Answers

Auto-generated code creates partial class.

public partial class Manager : EntityObject

So you just add one more file to the partial class like this:

public partial class Manager : IDoWork
{
   public string DoSomeWork()
   {
   }
}

Reference to MSDN.

How to: Customize Generated Data Objects

like image 55
Alex Aza Avatar answered Oct 15 '22 07:10

Alex Aza