Using multiple models in one controller

I used EF Designer from database to create my models from the database created for my project. For a single controller, I need to refer to multiple models. An example of this is the users table, which contains the user department and office. I need to reference two separate tables DepartmentPermissions and OfficePermissions to determine what data the user can see.

Here are examples of auto-generated models:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Project.Models
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public int User_ID { get; set; }
        public string User_Username { get; set; }
        public string User_Department { get; set; }
        public string User_Office { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Project.Models
{
    using System;
    using System.Collections.Generic;

    public partial class DepartmentPermission
    {
        public int DeptPerm_ID { get; set; }
        public string DeptPerm_Department { get; set; }
        public string DeptPerm_Role { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Project.Models
{
    using System;
    using System.Collections.Generic;

    public partial class OfficePermission
    {
        public string OffPerm_OfficeID { get; set; }
    }
}

      

The first column in each table is the primary key in the database.

When creating a controller using any of these autogenerated models, everything works fine, except that I can only use one table.

I want to be able to check the department and office of a user from the Users table, and then check the role associated with the user department from the DepartmentPermissions table, and then check if the user's office is in the OfficePermissions table.

I read that I can create a View Model to create a new model that combines the models that I want to use so that I can get information from all the relevant tables in one controller.

My ViewModel example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Project.Models
{
    public class HomeViewModel
    {            
        public IEnumerable<User> Users { get; set; }
        public IEnumerable<OfficePermission> OfficePermissions { get; set; }
        public IEnumerable<DepartmentPermission> DepartmentPermissions { get; set; } 
    }
}

      

When I try to create a controller with this viewmodel, I get errors:

User :: EntityType "User" has no key. Define a key for this entity type. OfficePermission :: EntityType "OfficePermission" has no key. Define a key for this entity type. DepartmentPermission :: EntityType 'DepartmentPermission' did not define a key. Define a key for this entity type. HomeViewModel :: EntityType "HomeViewModel" does not contain a key. Define a key for this entity type. Users: EntityType: EntitySet The Users are based on the User type, which has no specific keys. OfficePermissions: EntityType: EntitySet "OfficePermissions" is based on type "OfficePermission", which has no defined keys. DepartmentPermissions: EntityType: EntitySet 'DepartmentPermissions' based on type "DepartmentPermission ", which has no defined keys. HomeViewModels: EntityType: EntitySet" HomeViewModels "is based on the type" HomeViewModel ", which has no defined keys.

I can define Keys manually in autogenerated files, however this will be overridden when the database is modified and the files require autogeneration.

Is there a way to combine these autogenerated models to use them together in a controller?

0


source to share


1 answer


You have different options that you can use for any of them

Use ViewModel

For the view model, you need to create a class, and in this class, you define all the models as properties of that class. Here are two classes.

public class EmployeeDetails
{
    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

}

public class Employee
{
    public int Id { get; set; }
}

      

Here is the viewmodel

public class ViewModel
{
    public Employee emp { get; set; }
    public EmployeeDetails empdet{ get; set; }
}

      

Now in your controller you would do it like

public ActionResult About()
{
        ViewModel vm = new ViewModel();
        vm.emp = new Employee();
        vm.empdet = new EmployeeDetails();
        return View(vm);
}

      

And in the mind you get it like this



@model ViewModel

      

And as you stated you are getting the error table, the key is not defined, you must define the keys for the tables correctly.

Use Tuple

Tuple is used to store various types. You can save the desired class object in it and go to view

In the controller

Tuple<int, string> tuple = new Tuple<int, string>(1, "Hello world");
return View(tuple);

      

In sight you get it like this

@model Tuple<int,string>

      

+1


source







All Articles