none
How to make Data Models wherein there are multiple connections from one model to another RRS feed

  • Question

  • I'm trying to connect a model for employees to a survey. 

    The survey is requested by an employee(the supervisor).

    The survey is taken by an employee (co-worker)

    the survey is about an employee (employee)

    Right now my Employee Model looks like this

            public int EmployeeID { get; set; }
    
            public int SupervisorID { get; set; }
    
            [ForeignKey("SuperID")]
            public virtual Employee Supervisor { get; set; }
            //---Old-----
    
    
            public virtual ICollection<Employee> Supervisees { get; set; }
    
            
            public virtual ICollection<Survey> SurveysTakenByEmp { get; set; }
            public virtual ICollection<Survey> SurveysRequestedByEmp { get; set; }
            public virtual ICollection<Survey> SurveysTakenForEmp { get; set; }

    and my Survey model looks like this

            public int SurveyID { get; set; }
            public int ForEmpID { get; set; }
            public int TakenByEmpID { get; set; }
            public int RequestedByEmpID { get; set; }
    
    
            ///The employee that the survey is about
            [ForeignKey("ForEmpID")]
            public virtual Employee ForEmployee { get; set; }
    
            ///The employee who actually took the survey
            [ForeignKey("TakenByEmpID")]
            public virtual Employee TakenByEmployee { get; set; }
    
            ///The Employee who Requested that the survey be taken
            [ForeignKey("RequestedByEmpID")]
            public virtual Employee RequestedByEmployee { get; set; }
    

    Monday, February 3, 2014 2:21 PM

Answers

  • Hello,

    >>How to make Data Models wherein there are multiple connections from one model to another

    The entities classes is ok. Have a try to write the context like below:

    public class S04Context : DbContext
    
        {
    
            public DbSet<Employee> Employees { get; set; }
    
            public DbSet<Survey> Surveys { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Employee>().HasKey(e => e.EmployeeID).Property(a => a.EmployeeID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Survey>().HasKey(e => e.SurveyID).Property(a => a.SurveyID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                modelBuilder.Entity<Employee>().HasRequired(e => e.Supervisor).WithMany(s => s.Supervisees).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.ForEmployee).WithMany(s => s.SurveysTakenByEmp).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.TakenByEmployee).WithMany(s => s.SurveysRequestedByEmp).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.RequestedByEmployee).WithMany(s => s.SurveysTakenForEmp).WillCascadeOnDelete(false);
    
            }
    
        } 
    

    The created table schema:

    CREATE TABLE [dbo].[Employees] (
    
        [EmployeeID]            INT NOT NULL,
    
        [SupervisorID]          INT NOT NULL,
    
        [Supervisor_EmployeeID] INT NOT NULL,
    
        CONSTRAINT [PK_dbo.Employees] PRIMARY KEY CLUSTERED ([EmployeeID] ASC),
    
        CONSTRAINT [FK_dbo.Employees_dbo.Employees_Supervisor_EmployeeID] FOREIGN KEY ([Supervisor_EmployeeID]) REFERENCES [dbo].[Employees] ([EmployeeID])
    
    );
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_Supervisor_EmployeeID]
    
        ON [dbo].[Employees]([Supervisor_EmployeeID] ASC);
    
    
    CREATE TABLE [dbo].[Surveys] (
    
        [SurveyID]         INT NOT NULL,
    
        [ForEmpID]         INT NOT NULL,
    
        [TakenByEmpID]     INT NOT NULL,
    
        [RequestedByEmpID] INT NOT NULL,
    
        CONSTRAINT [PK_dbo.Surveys] PRIMARY KEY CLUSTERED ([SurveyID] ASC),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_ForEmpID] FOREIGN KEY ([ForEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID]),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_TakenByEmpID] FOREIGN KEY ([TakenByEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID]),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_RequestedByEmpID] FOREIGN KEY ([RequestedByEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID])
    
    );
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_ForEmpID]
    
        ON [dbo].[Surveys]([ForEmpID] ASC);
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_TakenByEmpID]
    
        ON [dbo].[Surveys]([TakenByEmpID] ASC);
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_RequestedByEmpID]
    
        ON [dbo].[Surveys]([RequestedByEmpID] ASC);
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by FixTheBroken Tuesday, February 4, 2014 4:20 PM
    Tuesday, February 4, 2014 3:34 AM
    Moderator

All replies

  • Hello,

    >>How to make Data Models wherein there are multiple connections from one model to another

    The entities classes is ok. Have a try to write the context like below:

    public class S04Context : DbContext
    
        {
    
            public DbSet<Employee> Employees { get; set; }
    
            public DbSet<Survey> Surveys { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Employee>().HasKey(e => e.EmployeeID).Property(a => a.EmployeeID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<Survey>().HasKey(e => e.SurveyID).Property(a => a.SurveyID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                modelBuilder.Entity<Employee>().HasRequired(e => e.Supervisor).WithMany(s => s.Supervisees).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.ForEmployee).WithMany(s => s.SurveysTakenByEmp).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.TakenByEmployee).WithMany(s => s.SurveysRequestedByEmp).WillCascadeOnDelete(false);
    
    
                modelBuilder.Entity<Survey>().HasRequired(e => e.RequestedByEmployee).WithMany(s => s.SurveysTakenForEmp).WillCascadeOnDelete(false);
    
            }
    
        } 
    

    The created table schema:

    CREATE TABLE [dbo].[Employees] (
    
        [EmployeeID]            INT NOT NULL,
    
        [SupervisorID]          INT NOT NULL,
    
        [Supervisor_EmployeeID] INT NOT NULL,
    
        CONSTRAINT [PK_dbo.Employees] PRIMARY KEY CLUSTERED ([EmployeeID] ASC),
    
        CONSTRAINT [FK_dbo.Employees_dbo.Employees_Supervisor_EmployeeID] FOREIGN KEY ([Supervisor_EmployeeID]) REFERENCES [dbo].[Employees] ([EmployeeID])
    
    );
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_Supervisor_EmployeeID]
    
        ON [dbo].[Employees]([Supervisor_EmployeeID] ASC);
    
    
    CREATE TABLE [dbo].[Surveys] (
    
        [SurveyID]         INT NOT NULL,
    
        [ForEmpID]         INT NOT NULL,
    
        [TakenByEmpID]     INT NOT NULL,
    
        [RequestedByEmpID] INT NOT NULL,
    
        CONSTRAINT [PK_dbo.Surveys] PRIMARY KEY CLUSTERED ([SurveyID] ASC),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_ForEmpID] FOREIGN KEY ([ForEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID]),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_TakenByEmpID] FOREIGN KEY ([TakenByEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID]),
    
        CONSTRAINT [FK_dbo.Surveys_dbo.Employees_RequestedByEmpID] FOREIGN KEY ([RequestedByEmpID]) REFERENCES [dbo].[Employees] ([EmployeeID])
    
    );
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_ForEmpID]
    
        ON [dbo].[Surveys]([ForEmpID] ASC);
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_TakenByEmpID]
    
        ON [dbo].[Surveys]([TakenByEmpID] ASC);
    
    
    
    GO
    
    CREATE NONCLUSTERED INDEX [IX_RequestedByEmpID]
    
        ON [dbo].[Surveys]([RequestedByEmpID] ASC);
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by FixTheBroken Tuesday, February 4, 2014 4:20 PM
    Tuesday, February 4, 2014 3:34 AM
    Moderator
  • Thanks! That did it. I've been a DB guy for a long time and I'm new to entity framework. Not very familiar with the DB context, so I was leaving what EF will generate.
    Tuesday, February 4, 2014 4:21 PM