【问题标题】:Using multiple models in a single controller [closed]在单个控制器中使用多个模型 [关闭]
【发布时间】:2015-02-15 12:12:07
【问题描述】:

我使用数据库中的 EF 设计器从为我的项目创建的数据库构建模型。 对于单个控制器,我需要引用多个模型。 这方面的一个例子是有一个包含用户部门和办公室的用户表。 我需要引用两个单独的表 DepartmentPermissions 和 OfficePermissions 以确定用户能够看到哪些数据。

以下是自动生成模型的示例:

//------------------------------------------------------------------------------
// <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; }
    }
}

每个表的第一列是数据库中的主键。

当使用这些自动生成的模型创建控制器时,一切正常,除了我只能使用一个表。

我希望能够从 Users 表中检查用户的部门和办公室,然后从 DepartmentPermissions 表中检查与用户部门关联的角色,然后检查用户的办公室是否在 OfficePermissions 表中。

我读到我可以构建一个视图模型来创建一个新模型,该模型组合了我想要使用的模型,以便我可以从一个控制器中的所有相关表中获取信息。

我的示例 ViewModel 是:

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; } 
    }
}

当我尝试使用此视图模型创建控制器时,我收到错误消息:

User:: EntityType 'User' 没有定义键。定义此实体类型的键。 OfficePermission:: EntityType 'OfficePermission' 没有定义键。定义此实体类型的键。 DepartmentPermission:: EntityType 'DepartmentPermission' 没有定义键。定义此实体类型的键。 HomeViewModel:: EntityType 'HomeViewModel' 没有定义键。定义此实体类型的键。 用户:EntityType:EntitySet 'Users' 基于没有定义键的类型'User'。 OfficePermissions:EntityType:EntitySet 'OfficePermissions' 基于没有定义键的类型 'OfficePermission'。 DepartmentPermissions:EntityType:EntitySet 'DepartmentPermissions' 基于没有定义键的类型'DepartmentPermission'。 HomeViewModels:EntityType:EntitySet 'HomeViewModels' 基于没有定义键的类型'HomeViewModel'。

我可以在自动生成的文件中手动定义密钥,但是当数据库更改并且文件需要自动重新生成时,这将被撤消。

有没有办法组合这些自动生成的模型以便在控制器中一起使用它们?

【问题讨论】:

  • 确保您的模型有一个唯一的键来区分每一行。
  • 显示你尝试过的代码!

标签: c# asp.net-mvc entity-framework viewmodel


【解决方案1】:

你有不同的选择,你可以使用它们中的任何一个

使用 ViewModel

对于视图模型,您必须创建一个类,在此类中您将定义所有模型作为该类的属性。这里有两个类。

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

}

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

这是视图模型

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

现在在 Controller 中你会这样做

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

你会收到这样的

@model ViewModel

正如您所说,您收到错误表没有定义键,您应该正确定义表的键。

使用元组

元组用于存储不同的类型。您可以将所需的类对象存储在其中并传递给视图

在控制器中

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

你会收到这样的

@model Tuple<int,string>

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多