问题描述
我已经使用数据库中的EF Designer从为我的项目创建的数据库构建我的模型。对于单个控制器,我需要引用多个模型。
一个例子是有一个Users表,其中包含用户部门和办公室。
我需要引用两个单独的表DepartmentPermissions和OfficePermissions来确定用户可以看到的数据。
以下是自动生成的模型的示例: / p>
// ------------------------- -------------------------------------------------- ---
//< auto-generated>
//该代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外的行为。
//如果重新生成代码,则对该文件的手动更改将被覆盖。
//< / auto-generated>
// -------------------------------------------- ----------------------------------
命名空间Project.Models
{
using System;
使用System.Collections.Generic;
public partial class User
{
public int User_ID {get;组; }
public string User_Username {get;组; }
public string User_Department {get;组; }
public string User_Office {get;组; }
}
}
// ----------------------------- -------------------------------------------------
//<自动生成>
//该代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外的行为。
//如果重新生成代码,则对该文件的手动更改将被覆盖。
//< / auto-generated>
// -------------------------------------------- ----------------------------------
命名空间Project.Models
{
using System;
使用System.Collections.Generic;
public partial class DepartmentPermission
{
public int DeptPerm_ID {get;组; }
public string DeptPerm_Department {get;组; }
public string DeptPerm_Role {get;组; }
}
}
// ----------------------------- -------------------------------------------------
//<自动生成>
//该代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外的行为。
//如果重新生成代码,则对该文件的手动更改将被覆盖。
//< / auto-generated>
// -------------------------------------------- ----------------------------------
命名空间Project.Models
{
using System;
使用System.Collections.Generic;
public partial class OfficePermission
{
public string OffPerm_OfficeID {get;组; }
}
}
每个表中的第一列是主键在数据库中。
当使用任何这些自动生成的模型创建控制器时,除了只能使用一个表之外,都可以正常工作。
我想从Users表中检查用户的部门和办公室,然后从DepartmentPermissions表中检查与用户部门关联的角色,然后检查用户的Office是否在OfficePermissions表中。
我读到,我可以构建一个视图模型来创建一个新的模型,结合我想要使用的模型,以便我可以从所有相关表中获取信息一个控制器。
我的示例ViewModel是:
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间Project.Models
{
public class HomeViewModel
{
public IEnumerable< User>用户{get;组; }
public IEnumerable< OfficePermission> OfficePermissions {get;组; }
public IEnumerable< DepartmentPermission> DepartmentPermissions {get;组; }
}
}
当我尝试用这个视图创建一个控制器模型,我收到错误说:
User :: EntityType'User'没有键定义。定义此实体类型的键。
OfficePermission :: EntityType'OfficePermission'没有定义键。定义此实体类型的键。
DepartmentPermission :: EntityType'DepartmentPermission'没有定义键。定义此实体类型的键。
HomeViewModel :: EntityType'HomeViewModel'没有定义键。定义此实体类型的键。
用户:EntityType:EntitySet'Users'基于没有定义键的类型User。
OfficePermissions:EntityType:EntitySetOfficePermissions基于没有定义键的类型OfficePermission。
DepartmentPermissions:EntityType:EntitySet'DepartmentPermissions'基于没有定义键的类型DepartmentPermission。
HomeViewModels:EntityType:EntitySet'HomeViewModels'基于没有定义键的HomeViewModel类型。
我可以在自动生成的文件中手动定义键,但是当数据库更改并且文件需要自动再生时,这将被撤消。
有没有办法组合这些自动生成的模型,以便在控制器中使用它们?
您有不同的选择,您可以使用任何人
使用ViewModel
对于视图模型,您必须创建一个类,并且在此类中,您将定义所有模型作为此类的属性。这是两个类。
public class EmployeeDetails
{
[必需]
[Display(Name =Name)]
public string Name {get;组; }
}
public class Employee
{
public int Id {get;组; }
}
这里是viewmodel
public class ViewModel
{
public Employee emp {get;组; }
public EmployeeDetails empdet {get;组;
}
现在在Controller中你会这样做
public ActionResult关于()
{
ViewModel vm = new ViewModel();
vm.emp = new Employee();
vm.empdet = new EmployeeDetails();
return View(vm);
}
看来你会收到这样的
@model ViewModel
正如你所说的,你得到错误表没有键定义你应该正确定义表的键。
使用元组
元组用于存储不同类型。您可以存储您所需的类对象在其中并传递到查看
在控制器中
元组< int,string> tuple = new Tuple< int,string>(1,Hello world);
return View(tuple);
看来你会收到这样的
@model Tuple< int,string>
I have used EF Designer from database to build my models from a database created for my project.For a single controller, I need to reference multiple models.An example of this is that there is a 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 is able to see.
Here are examples of the 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 a primary key in the database.
When creating a Controller using any of these autogenerated models all works fine, apart from I can only use one table.
I want to be able to check the user's department and office from the Users table and then check the Role associated with the user's department from the DepartmentPermissions table and then check whether the user's office is in the OfficePermissions table.
I read that I can build a View Model to create a new model which combines the models that I want to use so that I can get information from all relevant tables in the one controller.
My example ViewModel is:
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 attempt to create a Controller with this view model, I get errors saying:
User:: EntityType 'User' has no key defined. Define the key for this entity type.OfficePermission:: EntityType 'OfficePermission' has no key defined. Define the key for this entity type.DepartmentPermission:: EntityType 'DepartmentPermission' has no key defined. Define the key for this entity type.HomeViewModel:: EntityType 'HomeViewModel' has no key defined. Define the key for this entity type.Users: EntityType: EntitySet 'Users' is based on type 'User' that has no keys defined.OfficePermissions: EntityType: EntitySet 'OfficePermissions' is based on type 'OfficePermission' that has no keys defined.DepartmentPermissions: EntityType: EntitySet 'DepartmentPermissions' is based on type 'DepartmentPermission' that has no keys defined.HomeViewModels: EntityType: EntitySet 'HomeViewModels' is based on type 'HomeViewModel' that has no keys defined.
I could define Keys manually in the autogenerated files, however this would be undone when the database is changed and the files require automatic regeneration.
Is there a way to combine these autogenerated models in order to use them together in a controller?
You have different options you can use anyone of them
Use ViewModel
For view model you have to create a class and in this class you will define all models as properties of this 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 viewmodel
public class ViewModel
{
public Employee emp { get; set; }
public EmployeeDetails empdet{ get; set; }
}
Now in Controller you will do like this
public ActionResult About()
{
ViewModel vm = new ViewModel();
vm.emp = new Employee();
vm.empdet = new EmployeeDetails();
return View(vm);
}
And in view you will receive it like this
@model ViewModel
And as you have stated that you are getting error table has no key defined you should properly define keys for the tables.
Use Tuple
Tuple is used to store different types.You can store your required classes object in it and pass to view
In controller
Tuple<int, string> tuple = new Tuple<int, string>(1, "Hello world");
return View(tuple);
In view you will receive it like this
@model Tuple<int,string>
这篇关于在单个控制器中使用多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!