Skip to content

Commit

Permalink
INTRODUCTION TO ENTITY FRAMEWORK
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicTheCat committed Mar 1, 2019
1 parent a84dd66 commit c545c6e
Show file tree
Hide file tree
Showing 13 changed files with 5,555 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SoftUni
{
public class Configuration
{
public static string ConnectionString => "Server=.;Database=SoftUni;Integrated Security=True;";
}
}
184 changes: 184 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Data/SoftUniContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
namespace SoftUni.Data
{
using Microsoft.EntityFrameworkCore;
using SoftUni.Models;

public class SoftUniContext : DbContext
{
public SoftUniContext()
{
}

public SoftUniContext(DbContextOptions<SoftUniContext> options)
: base(options)
{
}

public DbSet<Address> Addresses { get; set; }

public DbSet<Department> Departments { get; set; }

public DbSet<Employee> Employees { get; set; }

public DbSet<EmployeeProject> EmployeesProjects { get; set; }

public DbSet<Project> Projects { get; set; }

public DbSet<Town> Towns { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(Configuration.ConnectionString);
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");

modelBuilder.Entity<Address>(entity =>
{
entity.HasKey(e => e.AddressId);

entity.Property(e => e.AddressId).HasColumnName("AddressID");

entity.Property(e => e.AddressText)
.IsRequired()
.HasMaxLength(100)
.IsUnicode(false);

entity.Property(e => e.TownId).HasColumnName("TownID");

entity.HasOne(d => d.Town)
.WithMany(p => p.Addresses)
.HasForeignKey(d => d.TownId)
.HasConstraintName("FK_Addresses_Towns");
});

modelBuilder.Entity<Department>(entity =>
{
entity.HasKey(e => e.DepartmentId);

entity.Property(e => e.DepartmentId).HasColumnName("DepartmentID");

entity.Property(e => e.ManagerId).HasColumnName("ManagerID");

entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);

entity.HasOne(d => d.Manager)
.WithMany(p => p.Departments)
.HasForeignKey(d => d.ManagerId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Departments_Employees");
});

modelBuilder.Entity<Employee>(entity =>
{
entity.HasKey(e => e.EmployeeId);

entity.Property(e => e.EmployeeId).HasColumnName("EmployeeID");

entity.Property(e => e.AddressId).HasColumnName("AddressID");

entity.Property(e => e.DepartmentId).HasColumnName("DepartmentID");

entity.Property(e => e.FirstName)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);

entity.Property(e => e.HireDate).HasColumnType("smalldatetime");

entity.Property(e => e.JobTitle)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);

entity.Property(e => e.LastName)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);

entity.Property(e => e.ManagerId).HasColumnName("ManagerID");

entity.Property(e => e.MiddleName)
.HasMaxLength(50)
.IsUnicode(false);

entity.Property(e => e.Salary).HasColumnType("decimal(15, 4)");

entity.HasOne(d => d.Address)
.WithMany(p => p.Employees)
.HasForeignKey(d => d.AddressId)
.HasConstraintName("FK_Employees_Addresses");

entity.HasOne(d => d.Department)
.WithMany(p => p.Employees)
.HasForeignKey(d => d.DepartmentId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Employees_Departments");

entity.HasOne(d => d.Manager)
.WithMany(p => p.InverseManager)
.HasForeignKey(d => d.ManagerId)
.HasConstraintName("FK_Employees_Employees");
});

modelBuilder.Entity<EmployeeProject>(entity =>
{
entity.HasKey(e => new { e.EmployeeId, e.ProjectId });

entity.Property(e => e.EmployeeId).HasColumnName("EmployeeID");

entity.Property(e => e.ProjectId).HasColumnName("ProjectID");

entity.HasOne(d => d.Employee)
.WithMany(p => p.EmployeesProjects)
.HasForeignKey(d => d.EmployeeId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_EmployeesProjects_Employees");

entity.HasOne(d => d.Project)
.WithMany(p => p.EmployeesProjects)
.HasForeignKey(d => d.ProjectId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_EmployeesProjects_Projects");
});

modelBuilder.Entity<Project>(entity =>
{
entity.HasKey(e => e.ProjectId);

entity.Property(e => e.ProjectId).HasColumnName("ProjectID");

entity.Property(e => e.Description).HasColumnType("ntext");

entity.Property(e => e.EndDate).HasColumnType("smalldatetime");

entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);

entity.Property(e => e.StartDate).HasColumnType("smalldatetime");
});

modelBuilder.Entity<Town>(entity =>
{
entity.HasKey(e => e.TownId);

entity.Property(e => e.TownId).HasColumnName("TownID");

entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
});
}
}
}
27 changes: 27 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Models/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace SoftUni.Models
{
using System.Collections.Generic;

public class Address
{
public Address()
{
Employees = new HashSet<Employee>();
}

public Address(string addressText, int townId)
{
this.AddressText = addressText;
this.TownId = townId;
}

public int AddressId { get; set; }

public string AddressText { get; set; }

public int? TownId { get; set; }
public Town Town { get; set; }

public ICollection<Employee> Employees { get; set; }
}
}
21 changes: 21 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Models/Department.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace SoftUni.Models
{
using System.Collections.Generic;

public class Department
{
public Department()
{
Employees = new HashSet<Employee>();
}

public int DepartmentId { get; set; }

public string Name { get; set; }

public int ManagerId { get; set; }
public Employee Manager { get; set; }

public ICollection<Employee> Employees { get; set; }
}
}
44 changes: 44 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Models/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace SoftUni.Models
{
using System;
using System.Collections.Generic;

public class Employee
{
public Employee()
{
Departments = new HashSet<Department>();
EmployeesProjects = new HashSet<EmployeeProject>();
InverseManager = new HashSet<Employee>();
}

public int EmployeeId { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string MiddleName { get; set; }

public string JobTitle { get; set; }

public int DepartmentId { get; set; }
public Department Department { get; set; }

public int? ManagerId { get; set; }
public Employee Manager { get; set; }

public DateTime HireDate { get; set; }

public decimal Salary { get; set; }

public int? AddressId { get; set; }
public Address Address { get; set; }

public ICollection<Employee> InverseManager { get; set; }

public ICollection<Department> Departments { get; set; }

public ICollection<EmployeeProject> EmployeesProjects { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SoftUni.Models
{
public class EmployeeProject
{
public int EmployeeId { get; set; }
public Employee Employee { get; set; }

public int ProjectId { get; set; }
public Project Project { get; set; }
}
}
25 changes: 25 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Models/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace SoftUni.Models
{
using System;
using System.Collections.Generic;

public class Project
{
public Project()
{
EmployeesProjects = new HashSet<EmployeeProject>();
}

public int ProjectId { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public DateTime StartDate { get; set; }

public DateTime? EndDate { get; set; }

public ICollection<EmployeeProject> EmployeesProjects { get; set; }
}
}
18 changes: 18 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/Models/Town.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SoftUni.Models
{
using System.Collections.Generic;

public class Town
{
public Town()
{
Addresses = new HashSet<Address>();
}

public int TownId { get; set; }

public string Name { get; set; }

public virtual ICollection<Address> Addresses { get; set; }
}
}
12 changes: 12 additions & 0 deletions 03.IntroductionToEntityFramework/AllExercises/SoftUni.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit c545c6e

Please sign in to comment.