Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.7 kB
3
Indexable
Never
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Company.Data.Entities;
using Company.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Company.Data.Contexts
{
    public class CompanyContext : DbContext
    {
        public DbSet<Companies> Companies => Set<Companies>();
        public DbSet<Departments> Departments => Set<Departments>();
        public DbSet<Employees> Employees => Set<Employees>();
        public DbSet<EmployeeRoles> EmployeeRoles => Set<EmployeeRoles>();
        public DbSet<Roles> Roles => Set<Roles>();

        public CompanyContext(DbContextOptions<CompanyContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder builder)
        { 
        base.OnModelCreating(builder); builder.Entity<EmployeeRoles>().HasKey(er => new {er.EmployeeId, er.RoleId });

            SeedData(builder);
        }
        private void SeedData(ModelBuilder builder)
        {
            var companies = new List<Companies>
            {

                 new Companies 
                 {
                    Id = 1,
                    CompanyName = "Sweden HQ",
                    OrganisationsId = 100,
                 },
                 new Companies 
                 {
                    Id = 2,
                    CompanyName = "Denmark",
                    OrganisationsId = 101,
                 },
                 new Companies 
                 {
                    Id = 3,
                    CompanyName = "Finland",
                    OrganisationsId = 102,
                 },

                 //{
                 //   Id = 4,
                 //   CompanyName = "Norway",
                 //   OrganisationsId = 103,
                 //},
             };
                builder.Entity<Companies>().HasData(companies);

            var departments = new List<Departments>
            { 
                new Departments
                {
                    Id = 1,
                    DepartmentName = "HR",
                    CompaniesId = 1,
                },

                new Departments
                {
                    Id = 2,
                    DepartmentName = "Sales",
                    CompaniesId = 2,

                },

                new Departments
                {
                    Id = 3,
                    DepartmentName = "Marketing",
                    CompaniesId = 3,

                },

                new Departments
                {
                    Id = 4,
                    DepartmentName = "Admin",
                    CompaniesId = 4,

                },


            };
            builder.Entity<Departments>().HasData(departments);

            var employees = new List<Employees>
            {
                new Employees
                {
                    Id = 1,
                    FirstName = "Kalle",
                    LastName = "Anka",
                    Salary = 500,
                    PartOfUnion = false,
                    DepartmentsId = 1
                },

            new Employees
                {
                    Id = 1,
                    FirstName = "Kajsa",
                    LastName = "Anka",
                    Salary = 600,
                    PartOfUnion = true,
                    DepartmentsId = 2
                },

            new Employees
                {
                    Id = 1,
                    FirstName = "Joakim",
                    LastName = "von Anka",
                    Salary = 50000,
                    PartOfUnion = false,
                    DepartmentsId = 2
                },

            new Employees
                {
                    Id = 1,
                    FirstName = "Musse",
                    LastName = "Pigg",
                    Salary = 700,
                    PartOfUnion = true,
                    DepartmentsId = 1
                },

            new Employees
                {
                    Id = 1,
                    FirstName = "Mimmi",
                    LastName = "Mus",
                    Salary = 800,
                    PartOfUnion = true,
                    DepartmentsId = 1
                },


            };
            builder.Entity<Employees>().HasData(employees);


            var employeeroles = new List<EmployeeRoles>
            {
                new EmployeeRoles
                {
                    EmployeeId = 1,
                    RoleId = 2,
                },

                new EmployeeRoles
                {
                    EmployeeId = 1,
                    RoleId = 2,
                },

                new EmployeeRoles
                {
                    EmployeeId = 2,
                    RoleId = 2,
                },

                new EmployeeRoles
                {
                    EmployeeId = 3,
                    RoleId = 3,
                },

                new EmployeeRoles
                {
                    EmployeeId = 3,
                    RoleId = 3,
                },

                new EmployeeRoles
                {
                    EmployeeId = 5,
                    RoleId = 4,
                },

                new EmployeeRoles
                {
                    EmployeeId = 6,
                    RoleId = 3,
                },

            };
            builder.Entity<EmployeeRoles>().HasData(employeeroles);

            var roles = new List<Roles>
            {
                new Roles
                {
                    Id = 1,
                    Role = "CEO",
                },

                new Roles
                {
                    Id = 2,
                    Role = "Manager",
                },

                new Roles
                {
                    Id = 3,
                    Role = "HR",
                },

                new Roles
                {
                    Id = 4,
                    Role = "Sales",
                },

                new Roles
                {
                    Id = 5,
                    Role = "Marketing",
                },

                new Roles
                {
                    Id = 6,
                    Role = "Admin",
                },

            };
            builder.Entity<Roles>().HasData(roles);



        }

        }
}