1

Could someone suggest a workaround for a C# limitation? I need for two different types to both derive from the same base type. In my example, Employees and Users are People. An Employee can also be a User (or vice versa). It seems that this is Multiple Inheritance, which is not supported in C#. I have reviewed http://www.codeproject.com/KB/architecture/smip.aspx, which doesn't seem to fit my scenario.

public abstract class Person
  {

    public int ID { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }
  }

  public class Employee : Person
  {

    public string Number { get; set; }

    public System.DateTime HireDate { get; set; }
  }

  public class User : Person
  {

    public string Password { get; set; }

    public bool Active { get; set; }
  }

  public class CfTestContext : DbContext
  {

    public DbSet<Employee> Employees { get; set; }

    public DbSet<User> Users { get; set; }

    public DbSet<Person> People { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

      modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

      modelBuilder.Entity<Person>().ToTable("People");

      modelBuilder.Entity<Employee>().ToTable("Employees");

      modelBuilder.Entity<User>().ToTable("Users");
    }
  }

  public class CreateCfTestDatabase : DropCreateDatabaseAlways<CfTestContext>
  {

    protected override void Seed(CfTestContext context)
    {

      // Abraham Lincoln hired as an Employee on February 12th.

      // Abraham doesn't need access as a User because he doesn't use the Purchasing application.

      context.Employees.Add(new Employee { Name = "Abraham Lincoln", Email = "[email protected]", Number = "107124", HireDate = System.DateTime.Parse("2/12/2000") });

      // George Washington added as a User on July 4th.

      // George is a consultant, so he will never become an Employee.

      context.Users.Add(new User { Name = "George Washington", Email = "[email protected]", Password = "xT76#a2", Active = true });

      context.SaveChanges();

      // Make Abraham Lincoln a User on September 29th.

      // Abraham received training and now needs to use the Purchasing application

      Employee employee = context.Employees.Where(t => t.Name == "Abraham Lincoln").FirstOrDefault();

      context.Users.Add(new User { Password = "C1x$av38", Active = true,  ID = employee.ID });  // this does not produce the desired results.

      context.SaveChanges();

      base.Seed(context);
    }
  }

1 Answer 1

1

You are right, there is no possibility to create multiple inheritance object in C#. But you can solve this issue by create a ComplexType in your model (eg. Login) and add the Password and Active property, the add a Login property to the person object. Then you can check whether the user has an active login information, by checking the Login.Active on the Person object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Espen for your timely answer.

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.