[英]Dealing with Master data table and Related tables in Entity Framework
I have 3 tables 我有3张桌子
Employee(ID,FirstName,LastName) - in which we have all the employees including managers etc., Employee(ID,FirstName,LastName)-我们拥有所有员工,包括经理等,
EmployeeRole(RoleID,Role) - Here we defined the role of the employees EmployeeRole(RoleID,Role)-在这里我们定义了员工的角色
Project(ProjectName,Manager,Employee,Date..) - here the details of the projects which are assigned to all the employees. Project(ProjectName,Manager,Employee,Date ..)-这里是分配给所有员工的项目的详细信息。
In the project table i have columns like Emmployee,Manager, both the columns are foreign key to the Employee table. 在项目表中,我有Emmployee,Manager之类的列,这两列都是Employee表的外键。 The problem is I having input like (Firstname,Lastname), how do i find the id of the emplyee.
问题是我输入了(Firstname,Lastname)之类的信息,我该如何找到emplyee的ID。 Or are the Table Structure is wrong?
还是表结构错误?
When i try to insert data in the Project Table, EmployeeRole table is also updating.It should not update the EMployeeRole Table. 当我尝试在项目表中插入数据时,EmployeeRole表也在更新。它不应更新EMployeeRole表。 its a master data.
它是一个主数据。
Please suggest me the solution? 请建议我解决方案?
Your logical data model is not correct (IMHO). 您的逻辑数据模型不正确(IMHO)。
A Project can have many Employees. 一个项目可以有很多员工。 And a Employee can work on one or more Projects as a particular Role.
员工可以作为一个特定角色从事一个或多个项目。
So it's a many to many between Employee and Project, with the intersection table having a Role type. 因此,员工和项目之间的关系是很多的,交集表具有角色类型。
eg 例如
CREATE TABLE [dbo].[Employee](
[EmployeeID] [int] NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmployeeID] ASC
))
GO
CREATE TABLE [dbo].[Project](
[ProjectID] [int] NOT NULL,
[ProjectName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED
(
[ProjectID] ASC
))
GO
CREATE TABLE [dbo].[ProjectEmployee](
[ProjectID] [int] NOT NULL,
[EmployeeID] [int] NOT NULL,
[RoleID] [int] NOT NULL,
CONSTRAINT [PK_ProjectEmployee] PRIMARY KEY CLUSTERED
(
[ProjectID] ASC,
[EmployeeID] ASC,
[RoleID] ASC
))
GO
CREATE TABLE [dbo].[Role](
[RoleID] [int] NOT NULL,
[RoleName] [int] NOT NULL,
CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED
(
[RoleID] ASC
))
GO
ALTER TABLE [dbo].[ProjectEmployee] WITH CHECK ADD CONSTRAINT [FK_ProjectEmployee_Employee] FOREIGN KEY([EmployeeID])
REFERENCES [dbo].[Employee] ([EmployeeID])
GO
ALTER TABLE [dbo].[ProjectEmployee] WITH CHECK ADD CONSTRAINT [FK_ProjectEmployee_Project] FOREIGN KEY([ProjectID])
REFERENCES [dbo].[Project] ([ProjectID])
GO
ALTER TABLE [dbo].[ProjectEmployee] WITH CHECK ADD CONSTRAINT [FK_ProjectEmployee_Role] FOREIGN KEY([RoleID])
REFERENCES [dbo].[Role] ([RoleID])
GO
Then your LinqToEntity looks like this: 然后,您的LinqToEntity如下所示:
// Add a new Role
Role role = new Role();
role.RoleID = 1; // TODO: make identity in database
role.RoleName = "Role 1";
db.Roles.Add(role);
db.SaveChanges();
// Add a new Employee
Employee employee = new Employee();
employee.EmployeeID = 1; // TODO: make identity in database
employee.FirstName = "Carl";
employee.LastName = "Prothman";
db.Employee.Add(employee);
db.SaveChanges();
// Add a new Project
Project project = new Project();
project.ProjectID = 1; // TODO: make identity in database
project.ProjectName = "Create new data model";
db.SaveChanges();
// Add employee to project as role1
ProjectEmployee projectEmployee = new ProjectEmployee();
projectEmployee.ProjectID = project.ProjectID;
projectEmployee.EmployeeID = employee.EmployeeID;
projectEmployee.RoleID = role.RoleID;
db.ProjectEmployees.Add(projectEmployee);
db.SaveChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.