Daniel K Rudolf_mag
Daniel K Rudolf_mag

Reputation: 287

Refer to more than one table in MVC

Can I call / use more than one table in my class?

e.g.

public class UserModel : UsersTable 

How can I call another table in this class, e.g. CustomerTable, something like this:

public class UserModel : UsersTable : CustomerTable

Upvotes: 1

Views: 60

Answers (1)

StuartLC
StuartLC

Reputation: 107247

If you want to re-use entity layer classes (e.g. Entity Framework objects) in your MVC ViewModels, I would use Composition, and not inheritance to achieve this, viz:

public class UserModel  // View Model
{
    public UsersTable UsersTable // EF POCO
    {
       get;
       set;
    }
    public CustomerTable CustomerTable
    {
      get;
      set;
    }

    // Other random data needed for your `.cshtml` / View
}

This way, you can extend your view model indefintely. As you've no doubt found, C# doesn't support multiple inheritance (or mixins / traits), so your options will be limited by inheritance (and it is arguably not logical, since a ViewModel isn't a DTO in the OO sense.)

Upvotes: 2

Related Questions