【发布时间】: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