2

I have 3 tables

Employee(ID,FirstName,LastName) - in which we have all the employees including managers etc.,

EmployeeRole(RoleID,Role) - Here we defined the role of the employees

Project(ProjectName,Manager,Employee,Date..) - here the details of the projects which are assigned to all the employees.

  1. In the project table i have columns like Emmployee,Manager, both the columns are foreign key to the Employee table. The problem is I having input like (Firstname,Lastname), how do i find the id of the emplyee. Or are the Table Structure is wrong?

  2. When i try to insert data in the Project Table, EmployeeRole table is also updating.It should not update the EMployeeRole Table. its a master data.

Please suggest me the solution?

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
Tanya
  • 1,571
  • 5
  • 22
  • 33

1 Answers1

0

Your logical data model is not correct (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.

e.g.

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:

// 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();
Carl Prothman
  • 1,461
  • 13
  • 23