Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is what i am trying to do.
1. A company is created which has mulitple Employees.
2. Employees create Events and also that the Employees are assigned to Events i.e.
Employees has many to many relationship with Events. (bridge table)
3. For assigned Event to Employee, it has start date, end date and name of the event.
4. For the assigned Event, now Employee must create Available time for the event. eg. if Event is from 1/1/2021 to 2/1/2020, then Employee should be able to create Available time for him/herself for that time period as he/she is assigned to that event.


How can i create this entity relationship in ENTITY FRAMEWORK, please help.

What I have tried:

1. Created Company class with ID and Navigational Property for Employees (because employees has list of Employees)

public class Company
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CompanyId { get; set; }
        public string Name { get; set; }

        public virtual IList<Employee> Employees { get; set; }
    }


2. Created Employees class with EmployeeID as PK

public class Employee
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EmployeeId { get; set; }
        public string Name { get; set; }

        public int CompanyId{ get; set; }
        [ForeignKey("CompanyId")]
        public Company Company { get; set; }

        public virtual IList<EmployeeEvents> EmployeeEvents { get; set; }
    }



3. Created Events class with EventID as PK

public class Event
   {
       [Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public int EventId { get; set; }
       public string Name { get; set; }
       public DateTime? Start{ get; set; }
       public DateTime? End{ get; set; }

       public virtual IList<EmployeeEvents> EmployeeEvents { get; set; }

   }


4. Created EmployeeEvents bridge class with EmployeeId and EventId as FK

public class EmployeeEvents
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int EmployeeEventId { get; set; }

        public int EmployeeId { get; set; }
        [ForeignKey("EmployeeId")]
        public Employee Employee { get; set; }

        public int EventId{ get; set; }
        [ForeignKey("EventId")]
        public Event Event { get; set; }
    }


Please help for the Available time class with appropriate relation and navigation property.
Posted
Updated 19-Jan-21 12:00pm
v4
Comments
Richard Deeming 19-Jan-21 4:37am    
Which "Entity Framework" are you using? The answer will differ depending on whether you're using EF6 or EF Core.
BishnuKarki 19-Jan-21 17:57pm    
EF6

1 solution

I would think you would know the "start and end dates" for a "planned" event; nullable database fields are a PITA.

Add the "available / required time" as "intersection" data to the "employee-event" entity / class. (The data applies to the "relationship").
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900