简体   繁体   English

在 ASP.NET MVC C# 中连接到多个表或连接两个表

[英]Connect to multiple tables or join two tables in ASP.NET MVC C#

Sorry if the title was confusing.对不起,如果标题令人困惑。

Lets say I have a model called Employee.假设我有一个名为 Employee 的模型。 Employee has a column called Departments. Employee 有一个名为 Departments 的列。 This allows them to view the calendar for that department.这允许他们查看该部门的日历。 But some Employees should be able to view the calendar for multiple departments, and it is different for every employee.但是有些员工应该可以查看多个部门的日历,而且每个员工的日历都不一样。

Because there are many different departments, I do not want to have the Employee table filled with a ton of new columns for each department.因为有许多不同的部门,我不想让 Employee 表为每个部门填充大量的新列。

I saw something about linking (not sure if that is the right word) two tables.我看到了一些关于链接(不确定这是否是正确的词)两个表的东西。 So I made a second model/table called AdditionalDepartments that has the AdditionalDepartmentsId, EmployeeId, and Department.所以我创建了第二个模型/表,称为 AdditionalDepartments,它具有 AdditionalDepartmentsId、EmployeeId 和 Department。

Say Joe was part of department A but can also view B and C. This is what employee looks like.假设 Joe 是 A 部门的一员,但也可以查看 B 和 C。这就是员工的样子。

EmployeeId | name | Department
-------------------------------
    1      | Joe  |  IT

and the AdditionalDepartments would look like...和 AdditionalDepartments 看起来像......

AdditionalDepartmentsId | EmployeeId | Department
--------------------------------------------------
        1               |      1     |  HR
        2               |      1     |  Sales

How would I go about combining these?我将如何结合这些? Or is there a better way to store additional department information rather than two tables?或者有没有更好的方法来存储额外的部门信息而不是两个表?

I ask because in my view, when they create or edit an employee, they use the employee database, but I want to be able to set additional departments on these views.我问是因为在我看来,当他们创建或编辑员工时,他们使用员工数据库,但我希望能够在这些视图上设置其他部门。 The problem with that is you can only use one model per view so I am having difficulties.问题是每个视图只能使用一个模型,所以我遇到了困难。

Note: I cannot drop the Department from Employee because it is used for a lot more than what the AdditionalDepartments would be used for.注意:我不能从 Employee 中删除 Department,因为它的用途远远超过 AdditionalDepartments 的用途。

You would want the Employee model to have an IEnumerable<Department> that contains all the Departments that employee is a part of.您可能希望 Employee 模型有一个IEnumerable<Department> ,其中包含该员工所属的所有部门。

On a somewhat unrelated note, if you have the ability to change the table schema at all, you could just drop the Department column in the first table, and make the second table just be a list of departments associated with employees based on the employeeID columns (they would have a foreign key relationship).在一个有点不相关的说明中,如果您完全能够更改表架构,您可以只删除第一个表中的Department列,并使第二个表只是与基于employeeID列的员工关联的部门列表(他们会有外键关系)。

Sorry if I misunderstood your question, but unless you are taking one database to be a single model, this should do the trick for you.抱歉,如果我误解了您的问题,但除非您将一个数据库作为单个模型,否则这应该对您有用。

Change your first table column to something like OfficialDepartment.将您的第一个表格列更改为 OfficialDepartment 之类的内容。 In your second table, drop the 'Additional' and just leave it as Departments.在您的第二个表格中,删除“附加”并将其保留为部门。 Add 'IT' to this table for 'Joe' as well.也将“IT”添加到“Joe”的此表中。 Then output the SQL using the statement below into a List<Departments> into your model and use as needed.然后使用下面的语句将 SQL 输出到List<Departments>到您的模型中并根据需要使用。

select A.Departments as [Departments]
from Employees E
join Departments D on E.EmployeeId = D.EmployeeId
where E.EmployeeId = <empId>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM