From f8eddd61cf5026bdd57b0de66694e124437b1bf0 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sat, 6 Apr 2024 21:23:37 +0200 Subject: [PATCH] Add CodeFirst examples for .NET 6 and 8 (#785) --- System.Linq.Dynamic.Core.sln | 62 + .../CodeFirst.ConsoleApp.csproj | 13 + src-examples/CodeFirst.ConsoleApp/Program.cs | 86 ++ .../CodeFirst.ConsoleApp8.csproj | 17 + .../CodeFirst.DataAccess.csproj | 37 + .../Configurations/ActorsConfiguration.cs | 54 + .../Configurations/ClientsConfiguration.cs | 33 + .../Configurations/CopiesConfiguration.cs | 46 + .../Configurations/EmployeesConfiguration.cs | 39 + .../Configurations/MoviesConfiguration.cs | 48 + .../Configurations/RentalsConfiguration.cs | 53 + .../Configurations/StarringConfiguration.cs | 65 ++ .../Context/PostgresDbContext.cs | 30 + .../Context/SqlServerDbContext.cs | 30 + .../Factories/PostgresDbContextFactory.cs | 20 + .../Factories/SqlServerDbContextFactory.cs | 20 + .../Interfaces/IRepository.cs | 19 + ...348_PostgreSqlInitialMigration.Designer.cs | 1022 +++++++++++++++++ ...220124205348_PostgreSqlInitialMigration.cs | 346 ++++++ .../PostgresDbContextModelSnapshot.cs | 1020 ++++++++++++++++ ...5405_SqlServerInitialMigration.Designer.cs | 1022 +++++++++++++++++ ...0220124205405_SqlServerInitialMigration.cs | 358 ++++++ .../SqlServerDbContextModelSnapshot.cs | 1020 ++++++++++++++++ .../CodeFirst.DataAccess/Models/Actors.cs | 28 + .../CodeFirst.DataAccess/Models/Clients.cs | 27 + .../CodeFirst.DataAccess/Models/Copies.cs | 28 + .../CodeFirst.DataAccess/Models/Employees.cs | 23 + .../CodeFirst.DataAccess/Models/Movies.cs | 36 + .../CodeFirst.DataAccess/Models/Rentals.cs | 27 + .../CodeFirst.DataAccess/Models/Starring.cs | 21 + .../Repositories/ActorsRepository.cs | 11 + .../Repositories/BaseRepository.cs | 69 ++ .../Repositories/ClientsRepository.cs | 11 + .../Repositories/CopiesRepository.cs | 11 + .../Repositories/EmployeesRepository.cs | 11 + .../Repositories/MoviesRepository.cs | 11 + .../Repositories/RentalsRepository.cs | 11 + .../Repositories/StarringRepository.cs | 11 + src-examples/README.md | 76 ++ 39 files changed, 5872 insertions(+) create mode 100644 src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.csproj create mode 100644 src-examples/CodeFirst.ConsoleApp/Program.cs create mode 100644 src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.csproj create mode 100644 src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.csproj create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/ActorsConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/ClientsConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/CopiesConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/EmployeesConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/MoviesConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/RentalsConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Configurations/StarringConfiguration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Context/PostgresDbContext.cs create mode 100644 src-examples/CodeFirst.DataAccess/Context/SqlServerDbContext.cs create mode 100644 src-examples/CodeFirst.DataAccess/Factories/PostgresDbContextFactory.cs create mode 100644 src-examples/CodeFirst.DataAccess/Factories/SqlServerDbContextFactory.cs create mode 100644 src-examples/CodeFirst.DataAccess/Interfaces/IRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.Designer.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/PostgresDbContextModelSnapshot.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.Designer.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.cs create mode 100644 src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/SqlServerDbContextModelSnapshot.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Actors.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Clients.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Copies.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Employees.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Movies.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Rentals.cs create mode 100644 src-examples/CodeFirst.DataAccess/Models/Starring.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/ActorsRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/BaseRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/ClientsRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/CopiesRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/EmployeesRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/MoviesRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/RentalsRepository.cs create mode 100644 src-examples/CodeFirst.DataAccess/Repositories/StarringRepository.cs create mode 100644 src-examples/README.md diff --git a/System.Linq.Dynamic.Core.sln b/System.Linq.Dynamic.Core.sln index 4d6a730c..896175b6 100644 --- a/System.Linq.Dynamic.Core.sln +++ b/System.Linq.Dynamic.Core.sln @@ -140,6 +140,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RadzenDataGrid.BlazorApp", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_net6.0_EF6_Sqlite", "src-console\ConsoleAppEF6_Sqlite\ConsoleApp_net6.0_EF6_Sqlite.csproj", "{CA03FD55-9DAB-4827-9A35-A96D3804B311}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src-examples", "src-examples", "{BCA2A024-9032-4E56-A6C4-17A15D921728}" + ProjectSection(SolutionItems) = preProject + src-examples\README.md = src-examples\README.md + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.DataAccess", "src-examples\CodeFirst.DataAccess\CodeFirst.DataAccess.csproj", "{E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.ConsoleApp", "src-examples\CodeFirst.ConsoleApp\CodeFirst.ConsoleApp.csproj", "{9E0D0994-7D84-40FF-8383-189F142FEF11}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeFirst.ConsoleApp8", "src-examples\CodeFirst.ConsoleApp8\CodeFirst.ConsoleApp8.csproj", "{68C7FF71-54F6-4D68-B419-65D1B10206D4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -858,6 +869,54 @@ Global {51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x64.Build.0 = Release|Any CPU {51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x86.ActiveCfg = Release|Any CPU {51074A4C-15C2-4E72-81F2-2FC53903553B}.Release|x86.Build.0 = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|ARM.ActiveCfg = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|ARM.Build.0 = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x64.ActiveCfg = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x64.Build.0 = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x86.ActiveCfg = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Debug|x86.Build.0 = Debug|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|Any CPU.Build.0 = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|ARM.ActiveCfg = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|ARM.Build.0 = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x64.ActiveCfg = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x64.Build.0 = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x86.ActiveCfg = Release|Any CPU + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4}.Release|x86.Build.0 = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|ARM.ActiveCfg = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|ARM.Build.0 = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x64.ActiveCfg = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x64.Build.0 = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x86.ActiveCfg = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Debug|x86.Build.0 = Debug|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|Any CPU.Build.0 = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|ARM.ActiveCfg = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|ARM.Build.0 = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x64.ActiveCfg = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x64.Build.0 = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x86.ActiveCfg = Release|Any CPU + {9E0D0994-7D84-40FF-8383-189F142FEF11}.Release|x86.Build.0 = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|ARM.ActiveCfg = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|ARM.Build.0 = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x64.ActiveCfg = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x64.Build.0 = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x86.ActiveCfg = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Debug|x86.Build.0 = Debug|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|Any CPU.Build.0 = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|ARM.ActiveCfg = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|ARM.Build.0 = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x64.ActiveCfg = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x64.Build.0 = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x86.ActiveCfg = Release|Any CPU + {68C7FF71-54F6-4D68-B419-65D1B10206D4}.Release|x86.Build.0 = Release|Any CPU {CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|Any CPU.Build.0 = Debug|Any CPU {CA03FD55-9DAB-4827-9A35-A96D3804B311}.Debug|ARM.ActiveCfg = Debug|Any CPU @@ -923,6 +982,9 @@ Global {9000129D-322D-4FE6-9C47-75464577C374} = {DBD7D9B6-FCC7-4650-91AF-E6457573A68F} {ABB1BF71-8927-49BB-99F3-70BCB2CD161E} = {8463ED7E-69FB-49AE-85CF-0791AFD98E38} {51074A4C-15C2-4E72-81F2-2FC53903553B} = {122BC4FA-7563-4E35-9D17-077F16F1629F} + {E36D1A08-F3ED-48C7-9DBF-8F625974A6C4} = {BCA2A024-9032-4E56-A6C4-17A15D921728} + {9E0D0994-7D84-40FF-8383-189F142FEF11} = {BCA2A024-9032-4E56-A6C4-17A15D921728} + {68C7FF71-54F6-4D68-B419-65D1B10206D4} = {BCA2A024-9032-4E56-A6C4-17A15D921728} {CA03FD55-9DAB-4827-9A35-A96D3804B311} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution diff --git a/src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.csproj b/src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.csproj new file mode 100644 index 00000000..47ee4626 --- /dev/null +++ b/src-examples/CodeFirst.ConsoleApp/CodeFirst.ConsoleApp.csproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0 + + + + + + + + diff --git a/src-examples/CodeFirst.ConsoleApp/Program.cs b/src-examples/CodeFirst.ConsoleApp/Program.cs new file mode 100644 index 00000000..449da120 --- /dev/null +++ b/src-examples/CodeFirst.ConsoleApp/Program.cs @@ -0,0 +1,86 @@ +using System; +using System.Diagnostics; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Threading.Tasks; +using CodeFirst.DataAccess.Factories; +using CodeFirst.DataAccess.Repositories; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.ConsoleApp; + +public static class Program +{ + private static async Task Main() + { + var sqlFactory = new SqlServerDbContextFactory(); + + await using var context = sqlFactory.CreateDbContext(Array.Empty()); + + var stopwatch = ValueStopwatch.StartNew(); + var query1 = context.Movies.Include(x => x.Copies).Where("Title.Contains(\"e\")").ToDynamicList(); + Console.WriteLine($"Elapsed time: {stopwatch.GetElapsedTime().TotalMilliseconds}ms"); + query1.ToList().ToList().ForEach(Console.WriteLine); + + Console.WriteLine(new string('-', 80)); + + stopwatch = ValueStopwatch.StartNew(); + var query2 = context.Movies.Include(x => x.Copies).Where("Title.Contains(\"e\")").ToDynamicList(); + Console.WriteLine($"Elapsed time: {stopwatch.GetElapsedTime().TotalMilliseconds}ms"); + query2.ToList().ToList().ForEach(Console.WriteLine); + + //var sqlServerRepo = new MoviesRepository(sqlFactory.CreateDbContext(Array.Empty())); + //var movies = await sqlServerRepo.GetAllAsync(); + //movies.ToList().ForEach(Console.WriteLine); + //var getById = await sqlServerRepo.GetByIdAsync(3); + //Console.WriteLine(getById.Title); + + //var postFactory = new PostgresDbContextFactory(); + //var postgresRepo = new MoviesRepository(postFactory.CreateDbContext(Array.Empty())); + //movies = await postgresRepo.GetAllAsync(); + //movies.ToList().ForEach(Console.WriteLine); + //getById = await postgresRepo.GetByIdAsync(3); + //Console.WriteLine(getById.Title); + } + + /// + /// Copied from https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ValueStopwatch/ValueStopwatch.cs + /// + internal readonly struct ValueStopwatch + { +#if !NET7_0_OR_GREATER + private static readonly double TimestampToTicks = TimeSpan.TicksPerSecond / (double)Stopwatch.Frequency; +#endif + + private readonly long _startTimestamp; + + public bool IsActive => _startTimestamp != 0; + + private ValueStopwatch(long startTimestamp) + { + _startTimestamp = startTimestamp; + } + + public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp()); + + public TimeSpan GetElapsedTime() + { + // Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0. + // So it being 0 is a clear indication of default(ValueStopwatch) + if (!IsActive) + { + throw new InvalidOperationException("An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time."); + } + + var end = Stopwatch.GetTimestamp(); + +#if !NET7_0_OR_GREATER + var timestampDelta = end - _startTimestamp; + var ticks = (long)(TimestampToTicks * timestampDelta); + return new TimeSpan(ticks); +#else + return Stopwatch.GetElapsedTime(_startTimestamp, end); +#endif + } + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.csproj b/src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.csproj new file mode 100644 index 00000000..94e18acd --- /dev/null +++ b/src-examples/CodeFirst.ConsoleApp8/CodeFirst.ConsoleApp8.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + + + + + + + + + + + + diff --git a/src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.csproj b/src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.csproj new file mode 100644 index 00000000..a23f9492 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/CodeFirst.DataAccess.csproj @@ -0,0 +1,37 @@ + + + + net6.0;net8.0 + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/src-examples/CodeFirst.DataAccess/Configurations/ActorsConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/ActorsConfiguration.cs new file mode 100644 index 00000000..d11d712a --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/ActorsConfiguration.cs @@ -0,0 +1,54 @@ +using System; +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class ActorsConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => e.ActorId) + .HasName("actor_pkey"); + builder.Property(e => e.ActorId) + .HasColumnName("actor_id"); + builder.Property(e => e.Firstname) + .HasColumnName("first_name"); + builder.Property(e => e.Lastname) + .HasColumnName("last_name"); + builder.Property(e => e.Birthday) + .HasColumnName("birthday"); + builder.ToTable("actors"); + + builder.HasData( + new Actors(1, "Arnold", "Schwarzenegger", Convert.ToDateTime("1947-07-30")), + new Actors(2, "Anthony", "Daniels", Convert.ToDateTime("1946-02-21")), + new Actors(3, "Harrison", "Ford", Convert.ToDateTime("1942-07-13")), + new Actors(4, "Carrie", "Fisher", Convert.ToDateTime("1956-10-21")), + new Actors(5, "Alec", "Guiness", Convert.ToDateTime("1914-04-02")), + new Actors(6, "Peter", "Cushing", Convert.ToDateTime("1913-05-26")), + new Actors(7, "David", "Prowse", Convert.ToDateTime("1944-05-19")), + new Actors(8, "Peter", "Mayhew", Convert.ToDateTime("1935-07-01")), + new Actors(9, "Michael", "Biehn", Convert.ToDateTime("1956-07-31")), + new Actors(10, "Linda", "Hamilton", Convert.ToDateTime("1956-09-26")), + new Actors(11, "Bill", "Murray", Convert.ToDateTime("1950-09-21")), + new Actors(12, "Dan", "Aykroyd", Convert.ToDateTime("1952-07-01")), + new Actors(13, "Sigourney", "Weaver", Convert.ToDateTime("1949-10-08")), + new Actors(14, "Robert", "De Niro", Convert.ToDateTime("1943-08-17")), + new Actors(15, "Jodie", "Foster", Convert.ToDateTime("1962-11-19")), + new Actors(16, "Harvey", "Keitel", Convert.ToDateTime("1939-05-13")), + new Actors(17, "Cybill", "Shepherd", Convert.ToDateTime("1950-02-18")), + new Actors(18, "Tom", "Berenger", Convert.ToDateTime("1949-05-31")), + new Actors(19, "Willem", "Dafoe", Convert.ToDateTime("1955-07-22")), + new Actors(20, "Charlie", "Sheen", Convert.ToDateTime("1965-09-03")), + new Actors(21, "Harrison", "Ford", Convert.ToDateTime("1942-07-13")), + new Actors(22, "Emmanuelle", "Seigner", Convert.ToDateTime("1966-06-22")), + new Actors(23, "Jean", "Reno", Convert.ToDateTime("1948-07-30")), + new Actors(24, "Billy", "Crystal", Convert.ToDateTime("1948-03-14")), + new Actors(25, "Lisa", "Kudrow", Convert.ToDateTime("1963-07-30")), + new Actors(26, "Gary", "Oldman", Convert.ToDateTime("1958-03-21")), + new Actors(27, "Natalie", "Portman", Convert.ToDateTime("1981-06-09")), + new Actors(28, "Tom", "Cruise", Convert.ToDateTime("1962-07-03"))); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/ClientsConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/ClientsConfiguration.cs new file mode 100644 index 00000000..5e269eec --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/ClientsConfiguration.cs @@ -0,0 +1,33 @@ +using System; +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class ClientsConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => e.ClientId) + .HasName("client_pkey"); + builder.Property(e => e.ClientId) + .HasColumnName("client_id"); + builder.Property(e => e.Firstname) + .HasColumnName("first_name"); + builder.Property(e => e.Lastname) + .HasColumnName("last_name"); + builder.Property(e => e.Birthday) + .HasColumnName("birthday"); + + builder.ToTable("clients"); + + builder.HasData( + new Clients(1, "Hank", "Hill", Convert.ToDateTime("1954-04-19")), + new Clients(2, "Brian", "Griffin", Convert.ToDateTime("2011-09-11")), + new Clients(3, "Gary", "Goodspeed", Convert.ToDateTime("1989-03-12")), + new Clients(4, "Bob", "Belcher", Convert.ToDateTime("1977-01-23")), + new Clients(5, "Lisa", "Simpson", Convert.ToDateTime("2012-05-09")), + new Clients(6, "Rick", "Sanchez", Convert.ToDateTime("1965-03-17"))); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/CopiesConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/CopiesConfiguration.cs new file mode 100644 index 00000000..59347949 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/CopiesConfiguration.cs @@ -0,0 +1,46 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class CopiesConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => e.CopyId) + .HasName("copies_pkey"); + + builder.Property(e => e.MovieId) + .IsRequired() + .HasColumnName("movie_id"); + + builder.Property(e => e.CopyId) + .HasColumnName("copy_id"); + + builder.Property(e => e.Available) + .HasColumnName("available"); + + builder.ToTable("copies"); + builder.HasData(new Copies(1, 1, true), + new Copies(2, 1, false), + new Copies(3, 2, true), + new Copies(4, 3, true), + new Copies(5, 3, false), + new Copies(6, 3, true), + new Copies(7, 4, true), + new Copies(8, 5, false), + new Copies(9, 6, true), + new Copies(10, 6, false), + new Copies(11, 6, true), + new Copies(12, 7, true), + new Copies(13, 7, true), + new Copies(14, 8, false), + new Copies(15, 9, true), + new Copies(16, 10, true), + new Copies(17, 10, false), + new Copies(18, 10, true), + new Copies(19, 10, true), + new Copies(20, 10, true)); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/EmployeesConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/EmployeesConfiguration.cs new file mode 100644 index 00000000..ca3062f9 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/EmployeesConfiguration.cs @@ -0,0 +1,39 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class EmployeesConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => e.EmployeeId) + .HasName("employee_id"); + builder.Property(e => e.EmployeeId) + .HasColumnName("employee_id"); + builder.Property(e => e.Firstname) + .IsRequired() + .HasColumnName("first_name"); + builder.Property(e => e.Lastname) + .IsRequired() + .HasColumnName("last_name"); + builder.Property(e => e.Salary) + .HasColumnName("salary"); + + builder.ToTable("employees"); + + builder.HasData( + new Employees + {EmployeeId = 1, Firstname = "John", Lastname = "Smith", Salary = 150.0f, City = "New York"}, + new Employees + {EmployeeId = 2, Firstname = "Ben", Lastname = "Johnson", Salary = 250.0f, City = "New York"}, + new Employees + {EmployeeId = 3, Firstname = "Louis", Lastname = "Armstrong", Salary = 75.0f, City = "New Orleans"}, + new Employees + {EmployeeId = 4, Firstname = "John", Lastname = "Lennon", Salary = 300.0f, City = "London"}, + new Employees + {EmployeeId = 5, Firstname = "Peter", Lastname = "Gabriel", Salary = 150.0f, City = "London"} + ); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/MoviesConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/MoviesConfiguration.cs new file mode 100644 index 00000000..1aa3d231 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/MoviesConfiguration.cs @@ -0,0 +1,48 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class MoviesConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(k => k.MovieId) + .HasName("movies_pkey"); + + // one to many movie - copies + builder.HasMany(e => e.Copies) + .WithOne(e => e.Movie); + + builder.Property(e => e.MovieId) + .ValueGeneratedOnAdd() + .HasColumnName("movie_id"); + + builder.Property(e => e.Title) + .HasColumnName("title"); + + builder.Property(e => e.AgeRestriction) + .HasColumnName("age_restriction"); + + builder.Property(e => e.Price) + .HasColumnName("price"); + + builder.Property(e => e.Year) + .HasColumnName("year"); + + builder.ToTable("movies"); + + builder.HasData( + new Movies(1, "Star Wars Episode IV: A New Hope", 1979, 12, 10f), + new Movies(2, "Ghostbusters", 1984, 12, 5.5f), + new Movies(3, "Terminator", 1984, 15, 8.5f), + new Movies(4, "Taxi Driver", 1976, 17, 5f), + new Movies(5, "Platoon", 1986, 18, 5f), + new Movies(6, "Frantic", 1988, 15, 8.5f), + new Movies(7, "Ronin", 1998, 13, 9.5f), + new Movies(8, "Analyze This", 1999, 16, 10.5f), + new Movies(9, "Leon: the Professional", 1994, 16, 8.5f), + new Movies(10, "Mission Impossible", 1996, 13, 8.5f)); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/RentalsConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/RentalsConfiguration.cs new file mode 100644 index 00000000..b1b905bf --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/RentalsConfiguration.cs @@ -0,0 +1,53 @@ +using System; +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class RentalsConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => new {e.ClientId, e.CopyId}) + .HasName("rentals_pkey"); + + builder.HasOne(e => e.Client) + .WithMany(e => e.Rentals) + .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey(e => e.ClientId); + builder.HasOne(e => e.Copy) + .WithMany(e => e.Rentals) + .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey(e => e.CopyId); + + builder.Property(e => e.ClientId) + .IsRequired() + .HasColumnName("client_id"); + builder.Property(e => e.CopyId) + .IsRequired() + .HasColumnName("copy_id"); + builder.Property(e => e.DateOfRental) + .HasColumnName("date_of_rental"); + builder.Property(e => e.DateOfReturn) + .HasColumnName("date_of_return"); + + builder.ToTable("rentals"); + + builder.HasData( + new Rentals(1, 1, Convert.ToDateTime("2005-07-04"), Convert.ToDateTime("2005-07-05")), + new Rentals(1, 6, Convert.ToDateTime("2005-07-19"), Convert.ToDateTime("2005-07-22")), + new Rentals(2, 3, Convert.ToDateTime("2005-07-24"), Convert.ToDateTime("2005-07-25")), + new Rentals(2, 5, Convert.ToDateTime("2005-07-26"), Convert.ToDateTime("2005-07-27")), + new Rentals(2, 7, Convert.ToDateTime("2005-07-29"), Convert.ToDateTime("2005-07-30")), + new Rentals(3, 12, Convert.ToDateTime("2005-07-10"), Convert.ToDateTime("2005-07-13")), + new Rentals(3, 20, Convert.ToDateTime("2005-07-16"), Convert.ToDateTime("2005-07-17")), + new Rentals(3, 3, Convert.ToDateTime("2005-07-22"), Convert.ToDateTime("2005-07-23")), + new Rentals(3, 7, Convert.ToDateTime("2005-07-24"), Convert.ToDateTime("2005-07-25")), + new Rentals(4, 13, Convert.ToDateTime("2005-07-01"), Convert.ToDateTime("2005-07-05")), + new Rentals(5, 11, Convert.ToDateTime("2005-07-10"), Convert.ToDateTime("2005-07-13")), + new Rentals(6, 1, Convert.ToDateTime("2005-07-06"), Convert.ToDateTime("2005-07-07")), + new Rentals(6, 7, Convert.ToDateTime("2005-07-29"), Convert.ToDateTime("2005-07-30")), + new Rentals(6, 19, Convert.ToDateTime("2005-07-29"), Convert.ToDateTime("2005-07-30"))); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Configurations/StarringConfiguration.cs b/src-examples/CodeFirst.DataAccess/Configurations/StarringConfiguration.cs new file mode 100644 index 00000000..a2077417 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Configurations/StarringConfiguration.cs @@ -0,0 +1,65 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace CodeFirst.DataAccess.Configurations; + +public class StarringConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(e => new {e.ActorId, e.MovieId}); + + // configure many to many: movies - actors + builder.HasOne(e => e.Movie) + .WithMany(e => e.Starring) + .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey(e => e.MovieId); + builder.HasOne(e => e.Actor) + .WithMany(e => e.Starring) + .OnDelete(DeleteBehavior.Cascade) + .HasForeignKey(e => e.ActorId); + + builder.Property(e => e.MovieId) + .IsRequired() + .HasColumnName("movie_id"); + + builder.Property(e => e.ActorId) + .IsRequired() + .HasColumnName("actor_id"); + + builder.ToTable("starring"); + + builder.HasData(new Starring(2, 1), + new Starring(3, 1), + new Starring(4, 1), + new Starring(5, 1), + new Starring(6, 1), + new Starring(7, 1), + new Starring(8, 1), + new Starring(1, 3), + new Starring(9, 3), + new Starring(10, 3), + new Starring(11, 2), + new Starring(12, 2), + new Starring(13, 2), + new Starring(14, 4), + new Starring(15, 4), + new Starring(16, 4), + new Starring(17, 4), + new Starring(18, 5), + new Starring(19, 5), + new Starring(20, 5), + new Starring(21, 6), + new Starring(22, 6), + new Starring(14, 7), + new Starring(23, 7), + new Starring(14, 8), + new Starring(24, 8), + new Starring(25, 8), + new Starring(23, 9), + new Starring(27, 9), + new Starring(23, 10), + new Starring(28, 10)); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Context/PostgresDbContext.cs b/src-examples/CodeFirst.DataAccess/Context/PostgresDbContext.cs new file mode 100644 index 00000000..efadd379 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Context/PostgresDbContext.cs @@ -0,0 +1,30 @@ +using CodeFirst.DataAccess.Configurations; +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Context; + +public class PostgresDbContext : DbContext +{ + public PostgresDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Movies { get; set; } + public DbSet Copies { get; set; } + public DbSet Starring { get; set; } + public DbSet Actors { get; set; } + public DbSet Rentals { get; set; } + public DbSet Employees { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfiguration(new MoviesConfiguration()); + modelBuilder.ApplyConfiguration(new CopiesConfiguration()); + modelBuilder.ApplyConfiguration(new ActorsConfiguration()); + modelBuilder.ApplyConfiguration(new StarringConfiguration()); + modelBuilder.ApplyConfiguration(new RentalsConfiguration()); + modelBuilder.ApplyConfiguration(new ClientsConfiguration()); + modelBuilder.ApplyConfiguration(new EmployeesConfiguration()); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Context/SqlServerDbContext.cs b/src-examples/CodeFirst.DataAccess/Context/SqlServerDbContext.cs new file mode 100644 index 00000000..0fa59ebb --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Context/SqlServerDbContext.cs @@ -0,0 +1,30 @@ +using CodeFirst.DataAccess.Configurations; +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Context; + +public class SqlServerDbContext : DbContext +{ + public SqlServerDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Movies { get; set; } + public DbSet Copies { get; set; } + public DbSet Starring { get; set; } + public DbSet Actors { get; set; } + public DbSet Rentals { get; set; } + public DbSet Employees { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.ApplyConfiguration(new MoviesConfiguration()); + modelBuilder.ApplyConfiguration(new CopiesConfiguration()); + modelBuilder.ApplyConfiguration(new ActorsConfiguration()); + modelBuilder.ApplyConfiguration(new StarringConfiguration()); + modelBuilder.ApplyConfiguration(new RentalsConfiguration()); + modelBuilder.ApplyConfiguration(new ClientsConfiguration()); + modelBuilder.ApplyConfiguration(new EmployeesConfiguration()); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Factories/PostgresDbContextFactory.cs b/src-examples/CodeFirst.DataAccess/Factories/PostgresDbContextFactory.cs new file mode 100644 index 00000000..820e80a4 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Factories/PostgresDbContextFactory.cs @@ -0,0 +1,20 @@ +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace CodeFirst.DataAccess.Factories; + +public class PostgresDbContextFactory : IDesignTimeDbContextFactory +{ + public PostgresDbContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + var connectionString = Environment.GetEnvironmentVariable("POSTGRES_MOVIES_LOCAL_CONNSTR"); + optionsBuilder.UseNpgsql(connectionString + ?? throw new NullReferenceException( + $"Connection string is not got from environment {nameof(connectionString)}")); + + return new PostgresDbContext(optionsBuilder.Options); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Factories/SqlServerDbContextFactory.cs b/src-examples/CodeFirst.DataAccess/Factories/SqlServerDbContextFactory.cs new file mode 100644 index 00000000..65f0ec83 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Factories/SqlServerDbContextFactory.cs @@ -0,0 +1,20 @@ +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace CodeFirst.DataAccess.Factories; + +public class SqlServerDbContextFactory : IDesignTimeDbContextFactory +{ + public SqlServerDbContext CreateDbContext(string[] args) + { + var optionsBuilder = new DbContextOptionsBuilder(); + var connectionString = Environment.GetEnvironmentVariable("SQLSERVER_MOVIES_LOCAL_CONNSTR"); + optionsBuilder.UseSqlServer(connectionString + ?? throw new NullReferenceException( + $"Connection string is not got from environment {nameof(connectionString)}")); + + return new SqlServerDbContext(optionsBuilder.Options); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Interfaces/IRepository.cs b/src-examples/CodeFirst.DataAccess/Interfaces/IRepository.cs new file mode 100644 index 00000000..a59142a2 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Interfaces/IRepository.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace CodeFirst.DataAccess.Interfaces; + +public interface IRepository +{ + Task AddAsync(T entity); + void Update(T entity); + void Delete(T entity); + void Delete(Expression> where); + Task GetByIdAsync(int id); + Task GetSingleAsync(Expression> where); + Task> GetAllAsync(); + Task> GetManyAsync(Expression> where); + Task SaveChangesAsync(); +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.Designer.cs b/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.Designer.cs new file mode 100644 index 00000000..b151cea8 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.Designer.cs @@ -0,0 +1,1022 @@ +// +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace CodeFirst.DataAccess.Migrations +{ + [DbContext(typeof(PostgresDbContext))] + [Migration("20220124205348_PostgreSqlInitialMigration")] + partial class PostgreSqlInitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Property("ActorId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("actor_id") + .UseIdentityByDefaultColumn(); + + b.Property("Birthday") + .HasColumnType("timestamp without time zone") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("text") + .HasColumnName("last_name"); + + b.HasKey("ActorId") + .HasName("actor_pkey"); + + b.ToTable("actors"); + + b.HasData( + new + { + ActorId = 1, + Birthday = new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Arnold", + Lastname = "Schwarzenegger" + }, + new + { + ActorId = 2, + Birthday = new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Anthony", + Lastname = "Daniels" + }, + new + { + ActorId = 3, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 4, + Birthday = new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Carrie", + Lastname = "Fisher" + }, + new + { + ActorId = 5, + Birthday = new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Alec", + Lastname = "Guiness" + }, + new + { + ActorId = 6, + Birthday = new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Cushing" + }, + new + { + ActorId = 7, + Birthday = new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "David", + Lastname = "Prowse" + }, + new + { + ActorId = 8, + Birthday = new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Mayhew" + }, + new + { + ActorId = 9, + Birthday = new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Michael", + Lastname = "Biehn" + }, + new + { + ActorId = 10, + Birthday = new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Linda", + Lastname = "Hamilton" + }, + new + { + ActorId = 11, + Birthday = new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bill", + Lastname = "Murray" + }, + new + { + ActorId = 12, + Birthday = new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Dan", + Lastname = "Aykroyd" + }, + new + { + ActorId = 13, + Birthday = new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Sigourney", + Lastname = "Weaver" + }, + new + { + ActorId = 14, + Birthday = new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Robert", + Lastname = "De Niro" + }, + new + { + ActorId = 15, + Birthday = new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jodie", + Lastname = "Foster" + }, + new + { + ActorId = 16, + Birthday = new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harvey", + Lastname = "Keitel" + }, + new + { + ActorId = 17, + Birthday = new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Cybill", + Lastname = "Shepherd" + }, + new + { + ActorId = 18, + Birthday = new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Berenger" + }, + new + { + ActorId = 19, + Birthday = new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Willem", + Lastname = "Dafoe" + }, + new + { + ActorId = 20, + Birthday = new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Charlie", + Lastname = "Sheen" + }, + new + { + ActorId = 21, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 22, + Birthday = new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Emmanuelle", + Lastname = "Seigner" + }, + new + { + ActorId = 23, + Birthday = new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jean", + Lastname = "Reno" + }, + new + { + ActorId = 24, + Birthday = new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Billy", + Lastname = "Crystal" + }, + new + { + ActorId = 25, + Birthday = new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Kudrow" + }, + new + { + ActorId = 26, + Birthday = new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Oldman" + }, + new + { + ActorId = 27, + Birthday = new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Natalie", + Lastname = "Portman" + }, + new + { + ActorId = 28, + Birthday = new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Cruise" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Property("ClientId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("client_id") + .UseIdentityByDefaultColumn(); + + b.Property("Birthday") + .HasColumnType("timestamp without time zone") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("text") + .HasColumnName("last_name"); + + b.HasKey("ClientId") + .HasName("client_pkey"); + + b.ToTable("clients"); + + b.HasData( + new + { + ClientId = 1, + Birthday = new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Hank", + Lastname = "Hill" + }, + new + { + ClientId = 2, + Birthday = new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Brian", + Lastname = "Griffin" + }, + new + { + ClientId = 3, + Birthday = new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Goodspeed" + }, + new + { + ClientId = 4, + Birthday = new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bob", + Lastname = "Belcher" + }, + new + { + ClientId = 5, + Birthday = new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Simpson" + }, + new + { + ClientId = 6, + Birthday = new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Rick", + Lastname = "Sanchez" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Property("CopyId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("copy_id") + .UseIdentityByDefaultColumn(); + + b.Property("Available") + .HasColumnType("boolean") + .HasColumnName("available"); + + b.Property("MovieId") + .HasColumnType("integer") + .HasColumnName("movie_id"); + + b.HasKey("CopyId") + .HasName("copies_pkey"); + + b.HasIndex("MovieId"); + + b.ToTable("copies"); + + b.HasData( + new + { + CopyId = 1, + Available = true, + MovieId = 1 + }, + new + { + CopyId = 2, + Available = false, + MovieId = 1 + }, + new + { + CopyId = 3, + Available = true, + MovieId = 2 + }, + new + { + CopyId = 4, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 5, + Available = false, + MovieId = 3 + }, + new + { + CopyId = 6, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 7, + Available = true, + MovieId = 4 + }, + new + { + CopyId = 8, + Available = false, + MovieId = 5 + }, + new + { + CopyId = 9, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 10, + Available = false, + MovieId = 6 + }, + new + { + CopyId = 11, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 12, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 13, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 14, + Available = false, + MovieId = 8 + }, + new + { + CopyId = 15, + Available = true, + MovieId = 9 + }, + new + { + CopyId = 16, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 17, + Available = false, + MovieId = 10 + }, + new + { + CopyId = 18, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 19, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 20, + Available = true, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Employees", b => + { + b.Property("EmployeeId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("employee_id") + .UseIdentityByDefaultColumn(); + + b.Property("City") + .HasColumnType("text"); + + b.Property("Firstname") + .IsRequired() + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .IsRequired() + .HasColumnType("text") + .HasColumnName("last_name"); + + b.Property("Salary") + .HasColumnType("real") + .HasColumnName("salary"); + + b.HasKey("EmployeeId") + .HasName("employee_id"); + + b.ToTable("employees"); + + b.HasData( + new + { + EmployeeId = 1, + City = "New York", + Firstname = "John", + Lastname = "Smith", + Salary = 150f + }, + new + { + EmployeeId = 2, + City = "New York", + Firstname = "Ben", + Lastname = "Johnson", + Salary = 250f + }, + new + { + EmployeeId = 3, + City = "New Orleans", + Firstname = "Louis", + Lastname = "Armstrong", + Salary = 75f + }, + new + { + EmployeeId = 4, + City = "London", + Firstname = "John", + Lastname = "Lennon", + Salary = 300f + }, + new + { + EmployeeId = 5, + City = "London", + Firstname = "Peter", + Lastname = "Gabriel", + Salary = 150f + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Property("MovieId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("movie_id") + .UseIdentityByDefaultColumn(); + + b.Property("AgeRestriction") + .HasColumnType("integer") + .HasColumnName("age_restriction"); + + b.Property("Price") + .HasColumnType("real") + .HasColumnName("price"); + + b.Property("Title") + .HasColumnType("text") + .HasColumnName("title"); + + b.Property("Year") + .HasColumnType("integer") + .HasColumnName("year"); + + b.HasKey("MovieId") + .HasName("movies_pkey"); + + b.ToTable("movies"); + + b.HasData( + new + { + MovieId = 1, + AgeRestriction = 12, + Price = 10f, + Title = "Star Wars Episode IV: A New Hope", + Year = 1979 + }, + new + { + MovieId = 2, + AgeRestriction = 12, + Price = 5.5f, + Title = "Ghostbusters", + Year = 1984 + }, + new + { + MovieId = 3, + AgeRestriction = 15, + Price = 8.5f, + Title = "Terminator", + Year = 1984 + }, + new + { + MovieId = 4, + AgeRestriction = 17, + Price = 5f, + Title = "Taxi Driver", + Year = 1976 + }, + new + { + MovieId = 5, + AgeRestriction = 18, + Price = 5f, + Title = "Platoon", + Year = 1986 + }, + new + { + MovieId = 6, + AgeRestriction = 15, + Price = 8.5f, + Title = "Frantic", + Year = 1988 + }, + new + { + MovieId = 7, + AgeRestriction = 13, + Price = 9.5f, + Title = "Ronin", + Year = 1998 + }, + new + { + MovieId = 8, + AgeRestriction = 16, + Price = 10.5f, + Title = "Analyze This", + Year = 1999 + }, + new + { + MovieId = 9, + AgeRestriction = 16, + Price = 8.5f, + Title = "Leon: the Professional", + Year = 1994 + }, + new + { + MovieId = 10, + AgeRestriction = 13, + Price = 8.5f, + Title = "Mission Impossible", + Year = 1996 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.Property("ClientId") + .HasColumnType("integer") + .HasColumnName("client_id"); + + b.Property("CopyId") + .HasColumnType("integer") + .HasColumnName("copy_id"); + + b.Property("DateOfRental") + .HasColumnType("timestamp without time zone") + .HasColumnName("date_of_rental"); + + b.Property("DateOfReturn") + .HasColumnType("timestamp without time zone") + .HasColumnName("date_of_return"); + + b.HasKey("ClientId", "CopyId") + .HasName("rentals_pkey"); + + b.HasIndex("CopyId"); + + b.ToTable("rentals"); + + b.HasData( + new + { + ClientId = 1, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 1, + CopyId = 6, + DateOfRental = new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 5, + DateOfRental = new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 12, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 20, + DateOfRental = new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 4, + CopyId = 13, + DateOfRental = new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 5, + CopyId = 11, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 19, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.Property("ActorId") + .HasColumnType("integer") + .HasColumnName("actor_id"); + + b.Property("MovieId") + .HasColumnType("integer") + .HasColumnName("movie_id"); + + b.HasKey("ActorId", "MovieId"); + + b.HasIndex("MovieId"); + + b.ToTable("starring"); + + b.HasData( + new + { + ActorId = 2, + MovieId = 1 + }, + new + { + ActorId = 3, + MovieId = 1 + }, + new + { + ActorId = 4, + MovieId = 1 + }, + new + { + ActorId = 5, + MovieId = 1 + }, + new + { + ActorId = 6, + MovieId = 1 + }, + new + { + ActorId = 7, + MovieId = 1 + }, + new + { + ActorId = 8, + MovieId = 1 + }, + new + { + ActorId = 1, + MovieId = 3 + }, + new + { + ActorId = 9, + MovieId = 3 + }, + new + { + ActorId = 10, + MovieId = 3 + }, + new + { + ActorId = 11, + MovieId = 2 + }, + new + { + ActorId = 12, + MovieId = 2 + }, + new + { + ActorId = 13, + MovieId = 2 + }, + new + { + ActorId = 14, + MovieId = 4 + }, + new + { + ActorId = 15, + MovieId = 4 + }, + new + { + ActorId = 16, + MovieId = 4 + }, + new + { + ActorId = 17, + MovieId = 4 + }, + new + { + ActorId = 18, + MovieId = 5 + }, + new + { + ActorId = 19, + MovieId = 5 + }, + new + { + ActorId = 20, + MovieId = 5 + }, + new + { + ActorId = 21, + MovieId = 6 + }, + new + { + ActorId = 22, + MovieId = 6 + }, + new + { + ActorId = 14, + MovieId = 7 + }, + new + { + ActorId = 23, + MovieId = 7 + }, + new + { + ActorId = 14, + MovieId = 8 + }, + new + { + ActorId = 24, + MovieId = 8 + }, + new + { + ActorId = 25, + MovieId = 8 + }, + new + { + ActorId = 23, + MovieId = 9 + }, + new + { + ActorId = 27, + MovieId = 9 + }, + new + { + ActorId = 23, + MovieId = 10 + }, + new + { + ActorId = 28, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Copies") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.HasOne("CodeFirst.Models.Models.Clients", "Client") + .WithMany("Rentals") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Copies", "Copy") + .WithMany("Rentals") + .HasForeignKey("CopyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Copy"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.HasOne("CodeFirst.Models.Models.Actors", "Actor") + .WithMany("Starring") + .HasForeignKey("ActorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Starring") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Actor"); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Navigation("Starring"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Navigation("Copies"); + + b.Navigation("Starring"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.cs b/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.cs new file mode 100644 index 00000000..4019be59 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/20220124205348_PostgreSqlInitialMigration.cs @@ -0,0 +1,346 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace CodeFirst.DataAccess.Migrations +{ + public partial class PostgreSqlInitialMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "actors", + columns: table => new + { + actor_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + first_name = table.Column(type: "text", nullable: true), + last_name = table.Column(type: "text", nullable: true), + birthday = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("actor_pkey", x => x.actor_id); + }); + + migrationBuilder.CreateTable( + name: "clients", + columns: table => new + { + client_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + first_name = table.Column(type: "text", nullable: true), + last_name = table.Column(type: "text", nullable: true), + birthday = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("client_pkey", x => x.client_id); + }); + + migrationBuilder.CreateTable( + name: "employees", + columns: table => new + { + employee_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + first_name = table.Column(type: "text", nullable: false), + last_name = table.Column(type: "text", nullable: false), + salary = table.Column(type: "real", nullable: true), + City = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("employee_id", x => x.employee_id); + }); + + migrationBuilder.CreateTable( + name: "movies", + columns: table => new + { + movie_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + title = table.Column(type: "text", nullable: true), + year = table.Column(type: "integer", nullable: false), + age_restriction = table.Column(type: "integer", nullable: false), + price = table.Column(type: "real", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("movies_pkey", x => x.movie_id); + }); + + migrationBuilder.CreateTable( + name: "copies", + columns: table => new + { + copy_id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + available = table.Column(type: "boolean", nullable: false), + movie_id = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("copies_pkey", x => x.copy_id); + table.ForeignKey( + name: "FK_copies_movies_movie_id", + column: x => x.movie_id, + principalTable: "movies", + principalColumn: "movie_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "starring", + columns: table => new + { + actor_id = table.Column(type: "integer", nullable: false), + movie_id = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_starring", x => new { x.actor_id, x.movie_id }); + table.ForeignKey( + name: "FK_starring_actors_actor_id", + column: x => x.actor_id, + principalTable: "actors", + principalColumn: "actor_id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_starring_movies_movie_id", + column: x => x.movie_id, + principalTable: "movies", + principalColumn: "movie_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "rentals", + columns: table => new + { + copy_id = table.Column(type: "integer", nullable: false), + client_id = table.Column(type: "integer", nullable: false), + date_of_rental = table.Column(type: "timestamp without time zone", nullable: true), + date_of_return = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("rentals_pkey", x => new { x.client_id, x.copy_id }); + table.ForeignKey( + name: "FK_rentals_clients_client_id", + column: x => x.client_id, + principalTable: "clients", + principalColumn: "client_id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_rentals_copies_copy_id", + column: x => x.copy_id, + principalTable: "copies", + principalColumn: "copy_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "actors", + columns: new[] { "actor_id", "birthday", "first_name", "last_name" }, + values: new object[,] + { + { 1, new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Arnold", "Schwarzenegger" }, + { 28, new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), "Tom", "Cruise" }, + { 27, new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), "Natalie", "Portman" }, + { 26, new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Gary", "Oldman" }, + { 24, new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), "Billy", "Crystal" }, + { 23, new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Jean", "Reno" }, + { 22, new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), "Emmanuelle", "Seigner" }, + { 21, new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harrison", "Ford" }, + { 20, new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), "Charlie", "Sheen" }, + { 19, new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), "Willem", "Dafoe" }, + { 18, new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), "Tom", "Berenger" }, + { 17, new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), "Cybill", "Shepherd" }, + { 16, new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harvey", "Keitel" }, + { 15, new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "Jodie", "Foster" }, + { 25, new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Lisa", "Kudrow" }, + { 13, new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), "Sigourney", "Weaver" }, + { 12, new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Dan", "Aykroyd" }, + { 11, new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bill", "Murray" }, + { 10, new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), "Linda", "Hamilton" }, + { 9, new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), "Michael", "Biehn" }, + { 8, new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Peter", "Mayhew" }, + { 7, new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "David", "Prowse" }, + { 6, new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), "Peter", "Cushing" }, + { 5, new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), "Alec", "Guiness" }, + { 4, new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Carrie", "Fisher" }, + { 3, new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harrison", "Ford" }, + { 2, new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Anthony", "Daniels" }, + { 14, new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), "Robert", "De Niro" } + }); + + migrationBuilder.InsertData( + table: "clients", + columns: new[] { "client_id", "birthday", "first_name", "last_name" }, + values: new object[,] + { + { 6, new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), "Rick", "Sanchez" }, + { 5, new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), "Lisa", "Simpson" }, + { 4, new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bob", "Belcher" }, + { 2, new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), "Brian", "Griffin" }, + { 1, new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "Hank", "Hill" }, + { 3, new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), "Gary", "Goodspeed" } + }); + + migrationBuilder.InsertData( + table: "employees", + columns: new[] { "employee_id", "City", "first_name", "last_name", "salary" }, + values: new object[,] + { + { 1, "New York", "John", "Smith", 150f }, + { 2, "New York", "Ben", "Johnson", 250f }, + { 3, "New Orleans", "Louis", "Armstrong", 75f }, + { 4, "London", "John", "Lennon", 300f }, + { 5, "London", "Peter", "Gabriel", 150f } + }); + + migrationBuilder.InsertData( + table: "movies", + columns: new[] { "movie_id", "age_restriction", "price", "title", "year" }, + values: new object[,] + { + { 8, 16, 10.5f, "Analyze This", 1999 }, + { 7, 13, 9.5f, "Ronin", 1998 }, + { 6, 15, 8.5f, "Frantic", 1988 }, + { 5, 18, 5f, "Platoon", 1986 }, + { 1, 12, 10f, "Star Wars Episode IV: A New Hope", 1979 }, + { 3, 15, 8.5f, "Terminator", 1984 }, + { 2, 12, 5.5f, "Ghostbusters", 1984 }, + { 9, 16, 8.5f, "Leon: the Professional", 1994 }, + { 4, 17, 5f, "Taxi Driver", 1976 }, + { 10, 13, 8.5f, "Mission Impossible", 1996 } + }); + + migrationBuilder.InsertData( + table: "copies", + columns: new[] { "copy_id", "available", "movie_id" }, + values: new object[,] + { + { 1, true, 1 }, + { 11, true, 6 }, + { 8, false, 5 }, + { 12, true, 7 }, + { 13, true, 7 }, + { 7, true, 4 }, + { 14, false, 8 }, + { 6, true, 3 }, + { 10, false, 6 }, + { 4, true, 3 }, + { 5, false, 3 }, + { 17, false, 10 }, + { 2, false, 1 }, + { 20, true, 10 }, + { 19, true, 10 }, + { 18, true, 10 }, + { 15, true, 9 }, + { 9, true, 6 }, + { 3, true, 2 }, + { 16, true, 10 } + }); + + migrationBuilder.InsertData( + table: "starring", + columns: new[] { "actor_id", "movie_id" }, + values: new object[,] + { + { 23, 7 }, + { 14, 8 }, + { 14, 7 }, + { 27, 9 }, + { 23, 9 }, + { 22, 6 }, + { 21, 6 }, + { 24, 8 }, + { 25, 8 }, + { 18, 5 }, + { 19, 5 }, + { 2, 1 }, + { 3, 1 }, + { 4, 1 }, + { 5, 1 }, + { 6, 1 }, + { 7, 1 }, + { 8, 1 }, + { 11, 2 }, + { 20, 5 }, + { 12, 2 }, + { 1, 3 }, + { 9, 3 }, + { 10, 3 }, + { 14, 4 }, + { 15, 4 }, + { 16, 4 }, + { 17, 4 }, + { 23, 10 }, + { 13, 2 }, + { 28, 10 } + }); + + migrationBuilder.InsertData( + table: "rentals", + columns: new[] { "client_id", "copy_id", "date_of_rental", "date_of_return" }, + values: new object[,] + { + { 1, 1, new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 1, new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 3, new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 3, new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 5, new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 1, 6, new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 7, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 7, new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 7, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 5, 11, new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 12, new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 4, 13, new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 19, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 20, new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) } + }); + + migrationBuilder.CreateIndex( + name: "IX_copies_movie_id", + table: "copies", + column: "movie_id"); + + migrationBuilder.CreateIndex( + name: "IX_rentals_copy_id", + table: "rentals", + column: "copy_id"); + + migrationBuilder.CreateIndex( + name: "IX_starring_movie_id", + table: "starring", + column: "movie_id"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "employees"); + + migrationBuilder.DropTable( + name: "rentals"); + + migrationBuilder.DropTable( + name: "starring"); + + migrationBuilder.DropTable( + name: "clients"); + + migrationBuilder.DropTable( + name: "copies"); + + migrationBuilder.DropTable( + name: "actors"); + + migrationBuilder.DropTable( + name: "movies"); + } + } +} diff --git a/src-examples/CodeFirst.DataAccess/Migrations/PostgresDbContextModelSnapshot.cs b/src-examples/CodeFirst.DataAccess/Migrations/PostgresDbContextModelSnapshot.cs new file mode 100644 index 00000000..4c8f6c48 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/PostgresDbContextModelSnapshot.cs @@ -0,0 +1,1020 @@ +// +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace CodeFirst.DataAccess.Migrations +{ + [DbContext(typeof(PostgresDbContext))] + partial class PostgresDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Property("ActorId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("actor_id") + .UseIdentityByDefaultColumn(); + + b.Property("Birthday") + .HasColumnType("timestamp without time zone") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("text") + .HasColumnName("last_name"); + + b.HasKey("ActorId") + .HasName("actor_pkey"); + + b.ToTable("actors"); + + b.HasData( + new + { + ActorId = 1, + Birthday = new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Arnold", + Lastname = "Schwarzenegger" + }, + new + { + ActorId = 2, + Birthday = new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Anthony", + Lastname = "Daniels" + }, + new + { + ActorId = 3, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 4, + Birthday = new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Carrie", + Lastname = "Fisher" + }, + new + { + ActorId = 5, + Birthday = new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Alec", + Lastname = "Guiness" + }, + new + { + ActorId = 6, + Birthday = new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Cushing" + }, + new + { + ActorId = 7, + Birthday = new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "David", + Lastname = "Prowse" + }, + new + { + ActorId = 8, + Birthday = new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Mayhew" + }, + new + { + ActorId = 9, + Birthday = new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Michael", + Lastname = "Biehn" + }, + new + { + ActorId = 10, + Birthday = new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Linda", + Lastname = "Hamilton" + }, + new + { + ActorId = 11, + Birthday = new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bill", + Lastname = "Murray" + }, + new + { + ActorId = 12, + Birthday = new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Dan", + Lastname = "Aykroyd" + }, + new + { + ActorId = 13, + Birthday = new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Sigourney", + Lastname = "Weaver" + }, + new + { + ActorId = 14, + Birthday = new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Robert", + Lastname = "De Niro" + }, + new + { + ActorId = 15, + Birthday = new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jodie", + Lastname = "Foster" + }, + new + { + ActorId = 16, + Birthday = new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harvey", + Lastname = "Keitel" + }, + new + { + ActorId = 17, + Birthday = new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Cybill", + Lastname = "Shepherd" + }, + new + { + ActorId = 18, + Birthday = new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Berenger" + }, + new + { + ActorId = 19, + Birthday = new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Willem", + Lastname = "Dafoe" + }, + new + { + ActorId = 20, + Birthday = new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Charlie", + Lastname = "Sheen" + }, + new + { + ActorId = 21, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 22, + Birthday = new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Emmanuelle", + Lastname = "Seigner" + }, + new + { + ActorId = 23, + Birthday = new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jean", + Lastname = "Reno" + }, + new + { + ActorId = 24, + Birthday = new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Billy", + Lastname = "Crystal" + }, + new + { + ActorId = 25, + Birthday = new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Kudrow" + }, + new + { + ActorId = 26, + Birthday = new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Oldman" + }, + new + { + ActorId = 27, + Birthday = new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Natalie", + Lastname = "Portman" + }, + new + { + ActorId = 28, + Birthday = new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Cruise" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Property("ClientId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("client_id") + .UseIdentityByDefaultColumn(); + + b.Property("Birthday") + .HasColumnType("timestamp without time zone") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("text") + .HasColumnName("last_name"); + + b.HasKey("ClientId") + .HasName("client_pkey"); + + b.ToTable("clients"); + + b.HasData( + new + { + ClientId = 1, + Birthday = new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Hank", + Lastname = "Hill" + }, + new + { + ClientId = 2, + Birthday = new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Brian", + Lastname = "Griffin" + }, + new + { + ClientId = 3, + Birthday = new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Goodspeed" + }, + new + { + ClientId = 4, + Birthday = new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bob", + Lastname = "Belcher" + }, + new + { + ClientId = 5, + Birthday = new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Simpson" + }, + new + { + ClientId = 6, + Birthday = new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Rick", + Lastname = "Sanchez" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Property("CopyId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("copy_id") + .UseIdentityByDefaultColumn(); + + b.Property("Available") + .HasColumnType("boolean") + .HasColumnName("available"); + + b.Property("MovieId") + .HasColumnType("integer") + .HasColumnName("movie_id"); + + b.HasKey("CopyId") + .HasName("copies_pkey"); + + b.HasIndex("MovieId"); + + b.ToTable("copies"); + + b.HasData( + new + { + CopyId = 1, + Available = true, + MovieId = 1 + }, + new + { + CopyId = 2, + Available = false, + MovieId = 1 + }, + new + { + CopyId = 3, + Available = true, + MovieId = 2 + }, + new + { + CopyId = 4, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 5, + Available = false, + MovieId = 3 + }, + new + { + CopyId = 6, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 7, + Available = true, + MovieId = 4 + }, + new + { + CopyId = 8, + Available = false, + MovieId = 5 + }, + new + { + CopyId = 9, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 10, + Available = false, + MovieId = 6 + }, + new + { + CopyId = 11, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 12, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 13, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 14, + Available = false, + MovieId = 8 + }, + new + { + CopyId = 15, + Available = true, + MovieId = 9 + }, + new + { + CopyId = 16, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 17, + Available = false, + MovieId = 10 + }, + new + { + CopyId = 18, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 19, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 20, + Available = true, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Employees", b => + { + b.Property("EmployeeId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("employee_id") + .UseIdentityByDefaultColumn(); + + b.Property("City") + .HasColumnType("text"); + + b.Property("Firstname") + .IsRequired() + .HasColumnType("text") + .HasColumnName("first_name"); + + b.Property("Lastname") + .IsRequired() + .HasColumnType("text") + .HasColumnName("last_name"); + + b.Property("Salary") + .HasColumnType("real") + .HasColumnName("salary"); + + b.HasKey("EmployeeId") + .HasName("employee_id"); + + b.ToTable("employees"); + + b.HasData( + new + { + EmployeeId = 1, + City = "New York", + Firstname = "John", + Lastname = "Smith", + Salary = 150f + }, + new + { + EmployeeId = 2, + City = "New York", + Firstname = "Ben", + Lastname = "Johnson", + Salary = 250f + }, + new + { + EmployeeId = 3, + City = "New Orleans", + Firstname = "Louis", + Lastname = "Armstrong", + Salary = 75f + }, + new + { + EmployeeId = 4, + City = "London", + Firstname = "John", + Lastname = "Lennon", + Salary = 300f + }, + new + { + EmployeeId = 5, + City = "London", + Firstname = "Peter", + Lastname = "Gabriel", + Salary = 150f + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Property("MovieId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("movie_id") + .UseIdentityByDefaultColumn(); + + b.Property("AgeRestriction") + .HasColumnType("integer") + .HasColumnName("age_restriction"); + + b.Property("Price") + .HasColumnType("real") + .HasColumnName("price"); + + b.Property("Title") + .HasColumnType("text") + .HasColumnName("title"); + + b.Property("Year") + .HasColumnType("integer") + .HasColumnName("year"); + + b.HasKey("MovieId") + .HasName("movies_pkey"); + + b.ToTable("movies"); + + b.HasData( + new + { + MovieId = 1, + AgeRestriction = 12, + Price = 10f, + Title = "Star Wars Episode IV: A New Hope", + Year = 1979 + }, + new + { + MovieId = 2, + AgeRestriction = 12, + Price = 5.5f, + Title = "Ghostbusters", + Year = 1984 + }, + new + { + MovieId = 3, + AgeRestriction = 15, + Price = 8.5f, + Title = "Terminator", + Year = 1984 + }, + new + { + MovieId = 4, + AgeRestriction = 17, + Price = 5f, + Title = "Taxi Driver", + Year = 1976 + }, + new + { + MovieId = 5, + AgeRestriction = 18, + Price = 5f, + Title = "Platoon", + Year = 1986 + }, + new + { + MovieId = 6, + AgeRestriction = 15, + Price = 8.5f, + Title = "Frantic", + Year = 1988 + }, + new + { + MovieId = 7, + AgeRestriction = 13, + Price = 9.5f, + Title = "Ronin", + Year = 1998 + }, + new + { + MovieId = 8, + AgeRestriction = 16, + Price = 10.5f, + Title = "Analyze This", + Year = 1999 + }, + new + { + MovieId = 9, + AgeRestriction = 16, + Price = 8.5f, + Title = "Leon: the Professional", + Year = 1994 + }, + new + { + MovieId = 10, + AgeRestriction = 13, + Price = 8.5f, + Title = "Mission Impossible", + Year = 1996 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.Property("ClientId") + .HasColumnType("integer") + .HasColumnName("client_id"); + + b.Property("CopyId") + .HasColumnType("integer") + .HasColumnName("copy_id"); + + b.Property("DateOfRental") + .HasColumnType("timestamp without time zone") + .HasColumnName("date_of_rental"); + + b.Property("DateOfReturn") + .HasColumnType("timestamp without time zone") + .HasColumnName("date_of_return"); + + b.HasKey("ClientId", "CopyId") + .HasName("rentals_pkey"); + + b.HasIndex("CopyId"); + + b.ToTable("rentals"); + + b.HasData( + new + { + ClientId = 1, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 1, + CopyId = 6, + DateOfRental = new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 5, + DateOfRental = new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 12, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 20, + DateOfRental = new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 4, + CopyId = 13, + DateOfRental = new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 5, + CopyId = 11, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 19, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.Property("ActorId") + .HasColumnType("integer") + .HasColumnName("actor_id"); + + b.Property("MovieId") + .HasColumnType("integer") + .HasColumnName("movie_id"); + + b.HasKey("ActorId", "MovieId"); + + b.HasIndex("MovieId"); + + b.ToTable("starring"); + + b.HasData( + new + { + ActorId = 2, + MovieId = 1 + }, + new + { + ActorId = 3, + MovieId = 1 + }, + new + { + ActorId = 4, + MovieId = 1 + }, + new + { + ActorId = 5, + MovieId = 1 + }, + new + { + ActorId = 6, + MovieId = 1 + }, + new + { + ActorId = 7, + MovieId = 1 + }, + new + { + ActorId = 8, + MovieId = 1 + }, + new + { + ActorId = 1, + MovieId = 3 + }, + new + { + ActorId = 9, + MovieId = 3 + }, + new + { + ActorId = 10, + MovieId = 3 + }, + new + { + ActorId = 11, + MovieId = 2 + }, + new + { + ActorId = 12, + MovieId = 2 + }, + new + { + ActorId = 13, + MovieId = 2 + }, + new + { + ActorId = 14, + MovieId = 4 + }, + new + { + ActorId = 15, + MovieId = 4 + }, + new + { + ActorId = 16, + MovieId = 4 + }, + new + { + ActorId = 17, + MovieId = 4 + }, + new + { + ActorId = 18, + MovieId = 5 + }, + new + { + ActorId = 19, + MovieId = 5 + }, + new + { + ActorId = 20, + MovieId = 5 + }, + new + { + ActorId = 21, + MovieId = 6 + }, + new + { + ActorId = 22, + MovieId = 6 + }, + new + { + ActorId = 14, + MovieId = 7 + }, + new + { + ActorId = 23, + MovieId = 7 + }, + new + { + ActorId = 14, + MovieId = 8 + }, + new + { + ActorId = 24, + MovieId = 8 + }, + new + { + ActorId = 25, + MovieId = 8 + }, + new + { + ActorId = 23, + MovieId = 9 + }, + new + { + ActorId = 27, + MovieId = 9 + }, + new + { + ActorId = 23, + MovieId = 10 + }, + new + { + ActorId = 28, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Copies") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.HasOne("CodeFirst.Models.Models.Clients", "Client") + .WithMany("Rentals") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Copies", "Copy") + .WithMany("Rentals") + .HasForeignKey("CopyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Copy"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.HasOne("CodeFirst.Models.Models.Actors", "Actor") + .WithMany("Starring") + .HasForeignKey("ActorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Starring") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Actor"); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Navigation("Starring"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Navigation("Copies"); + + b.Navigation("Starring"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.Designer.cs b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.Designer.cs new file mode 100644 index 00000000..09330763 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.Designer.cs @@ -0,0 +1,1022 @@ +// +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace CodeFirst.DataAccess.Migrations.SqlServerDb +{ + [DbContext(typeof(SqlServerDbContext))] + [Migration("20220124205405_SqlServerInitialMigration")] + partial class SqlServerInitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Property("ActorId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("actor_id") + .UseIdentityColumn(); + + b.Property("Birthday") + .HasColumnType("datetime2") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.HasKey("ActorId") + .HasName("actor_pkey"); + + b.ToTable("actors"); + + b.HasData( + new + { + ActorId = 1, + Birthday = new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Arnold", + Lastname = "Schwarzenegger" + }, + new + { + ActorId = 2, + Birthday = new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Anthony", + Lastname = "Daniels" + }, + new + { + ActorId = 3, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 4, + Birthday = new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Carrie", + Lastname = "Fisher" + }, + new + { + ActorId = 5, + Birthday = new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Alec", + Lastname = "Guiness" + }, + new + { + ActorId = 6, + Birthday = new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Cushing" + }, + new + { + ActorId = 7, + Birthday = new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "David", + Lastname = "Prowse" + }, + new + { + ActorId = 8, + Birthday = new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Mayhew" + }, + new + { + ActorId = 9, + Birthday = new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Michael", + Lastname = "Biehn" + }, + new + { + ActorId = 10, + Birthday = new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Linda", + Lastname = "Hamilton" + }, + new + { + ActorId = 11, + Birthday = new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bill", + Lastname = "Murray" + }, + new + { + ActorId = 12, + Birthday = new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Dan", + Lastname = "Aykroyd" + }, + new + { + ActorId = 13, + Birthday = new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Sigourney", + Lastname = "Weaver" + }, + new + { + ActorId = 14, + Birthday = new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Robert", + Lastname = "De Niro" + }, + new + { + ActorId = 15, + Birthday = new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jodie", + Lastname = "Foster" + }, + new + { + ActorId = 16, + Birthday = new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harvey", + Lastname = "Keitel" + }, + new + { + ActorId = 17, + Birthday = new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Cybill", + Lastname = "Shepherd" + }, + new + { + ActorId = 18, + Birthday = new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Berenger" + }, + new + { + ActorId = 19, + Birthday = new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Willem", + Lastname = "Dafoe" + }, + new + { + ActorId = 20, + Birthday = new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Charlie", + Lastname = "Sheen" + }, + new + { + ActorId = 21, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 22, + Birthday = new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Emmanuelle", + Lastname = "Seigner" + }, + new + { + ActorId = 23, + Birthday = new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jean", + Lastname = "Reno" + }, + new + { + ActorId = 24, + Birthday = new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Billy", + Lastname = "Crystal" + }, + new + { + ActorId = 25, + Birthday = new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Kudrow" + }, + new + { + ActorId = 26, + Birthday = new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Oldman" + }, + new + { + ActorId = 27, + Birthday = new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Natalie", + Lastname = "Portman" + }, + new + { + ActorId = 28, + Birthday = new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Cruise" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Property("ClientId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("client_id") + .UseIdentityColumn(); + + b.Property("Birthday") + .HasColumnType("datetime2") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.HasKey("ClientId") + .HasName("client_pkey"); + + b.ToTable("clients"); + + b.HasData( + new + { + ClientId = 1, + Birthday = new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Hank", + Lastname = "Hill" + }, + new + { + ClientId = 2, + Birthday = new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Brian", + Lastname = "Griffin" + }, + new + { + ClientId = 3, + Birthday = new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Goodspeed" + }, + new + { + ClientId = 4, + Birthday = new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bob", + Lastname = "Belcher" + }, + new + { + ClientId = 5, + Birthday = new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Simpson" + }, + new + { + ClientId = 6, + Birthday = new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Rick", + Lastname = "Sanchez" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Property("CopyId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("copy_id") + .UseIdentityColumn(); + + b.Property("Available") + .HasColumnType("bit") + .HasColumnName("available"); + + b.Property("MovieId") + .HasColumnType("int") + .HasColumnName("movie_id"); + + b.HasKey("CopyId") + .HasName("copies_pkey"); + + b.HasIndex("MovieId"); + + b.ToTable("copies"); + + b.HasData( + new + { + CopyId = 1, + Available = true, + MovieId = 1 + }, + new + { + CopyId = 2, + Available = false, + MovieId = 1 + }, + new + { + CopyId = 3, + Available = true, + MovieId = 2 + }, + new + { + CopyId = 4, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 5, + Available = false, + MovieId = 3 + }, + new + { + CopyId = 6, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 7, + Available = true, + MovieId = 4 + }, + new + { + CopyId = 8, + Available = false, + MovieId = 5 + }, + new + { + CopyId = 9, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 10, + Available = false, + MovieId = 6 + }, + new + { + CopyId = 11, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 12, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 13, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 14, + Available = false, + MovieId = 8 + }, + new + { + CopyId = 15, + Available = true, + MovieId = 9 + }, + new + { + CopyId = 16, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 17, + Available = false, + MovieId = 10 + }, + new + { + CopyId = 18, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 19, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 20, + Available = true, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Employees", b => + { + b.Property("EmployeeId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("employee_id") + .UseIdentityColumn(); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + b.Property("Firstname") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.Property("Salary") + .HasColumnType("real") + .HasColumnName("salary"); + + b.HasKey("EmployeeId") + .HasName("employee_id"); + + b.ToTable("employees"); + + b.HasData( + new + { + EmployeeId = 1, + City = "New York", + Firstname = "John", + Lastname = "Smith", + Salary = 150f + }, + new + { + EmployeeId = 2, + City = "New York", + Firstname = "Ben", + Lastname = "Johnson", + Salary = 250f + }, + new + { + EmployeeId = 3, + City = "New Orleans", + Firstname = "Louis", + Lastname = "Armstrong", + Salary = 75f + }, + new + { + EmployeeId = 4, + City = "London", + Firstname = "John", + Lastname = "Lennon", + Salary = 300f + }, + new + { + EmployeeId = 5, + City = "London", + Firstname = "Peter", + Lastname = "Gabriel", + Salary = 150f + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Property("MovieId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("movie_id") + .UseIdentityColumn(); + + b.Property("AgeRestriction") + .HasColumnType("int") + .HasColumnName("age_restriction"); + + b.Property("Price") + .HasColumnType("real") + .HasColumnName("price"); + + b.Property("Title") + .HasColumnType("nvarchar(max)") + .HasColumnName("title"); + + b.Property("Year") + .HasColumnType("int") + .HasColumnName("year"); + + b.HasKey("MovieId") + .HasName("movies_pkey"); + + b.ToTable("movies"); + + b.HasData( + new + { + MovieId = 1, + AgeRestriction = 12, + Price = 10f, + Title = "Star Wars Episode IV: A New Hope", + Year = 1979 + }, + new + { + MovieId = 2, + AgeRestriction = 12, + Price = 5.5f, + Title = "Ghostbusters", + Year = 1984 + }, + new + { + MovieId = 3, + AgeRestriction = 15, + Price = 8.5f, + Title = "Terminator", + Year = 1984 + }, + new + { + MovieId = 4, + AgeRestriction = 17, + Price = 5f, + Title = "Taxi Driver", + Year = 1976 + }, + new + { + MovieId = 5, + AgeRestriction = 18, + Price = 5f, + Title = "Platoon", + Year = 1986 + }, + new + { + MovieId = 6, + AgeRestriction = 15, + Price = 8.5f, + Title = "Frantic", + Year = 1988 + }, + new + { + MovieId = 7, + AgeRestriction = 13, + Price = 9.5f, + Title = "Ronin", + Year = 1998 + }, + new + { + MovieId = 8, + AgeRestriction = 16, + Price = 10.5f, + Title = "Analyze This", + Year = 1999 + }, + new + { + MovieId = 9, + AgeRestriction = 16, + Price = 8.5f, + Title = "Leon: the Professional", + Year = 1994 + }, + new + { + MovieId = 10, + AgeRestriction = 13, + Price = 8.5f, + Title = "Mission Impossible", + Year = 1996 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.Property("ClientId") + .HasColumnType("int") + .HasColumnName("client_id"); + + b.Property("CopyId") + .HasColumnType("int") + .HasColumnName("copy_id"); + + b.Property("DateOfRental") + .HasColumnType("datetime2") + .HasColumnName("date_of_rental"); + + b.Property("DateOfReturn") + .HasColumnType("datetime2") + .HasColumnName("date_of_return"); + + b.HasKey("ClientId", "CopyId") + .HasName("rentals_pkey"); + + b.HasIndex("CopyId"); + + b.ToTable("rentals"); + + b.HasData( + new + { + ClientId = 1, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 1, + CopyId = 6, + DateOfRental = new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 5, + DateOfRental = new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 12, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 20, + DateOfRental = new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 4, + CopyId = 13, + DateOfRental = new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 5, + CopyId = 11, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 19, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.Property("ActorId") + .HasColumnType("int") + .HasColumnName("actor_id"); + + b.Property("MovieId") + .HasColumnType("int") + .HasColumnName("movie_id"); + + b.HasKey("ActorId", "MovieId"); + + b.HasIndex("MovieId"); + + b.ToTable("starring"); + + b.HasData( + new + { + ActorId = 2, + MovieId = 1 + }, + new + { + ActorId = 3, + MovieId = 1 + }, + new + { + ActorId = 4, + MovieId = 1 + }, + new + { + ActorId = 5, + MovieId = 1 + }, + new + { + ActorId = 6, + MovieId = 1 + }, + new + { + ActorId = 7, + MovieId = 1 + }, + new + { + ActorId = 8, + MovieId = 1 + }, + new + { + ActorId = 1, + MovieId = 3 + }, + new + { + ActorId = 9, + MovieId = 3 + }, + new + { + ActorId = 10, + MovieId = 3 + }, + new + { + ActorId = 11, + MovieId = 2 + }, + new + { + ActorId = 12, + MovieId = 2 + }, + new + { + ActorId = 13, + MovieId = 2 + }, + new + { + ActorId = 14, + MovieId = 4 + }, + new + { + ActorId = 15, + MovieId = 4 + }, + new + { + ActorId = 16, + MovieId = 4 + }, + new + { + ActorId = 17, + MovieId = 4 + }, + new + { + ActorId = 18, + MovieId = 5 + }, + new + { + ActorId = 19, + MovieId = 5 + }, + new + { + ActorId = 20, + MovieId = 5 + }, + new + { + ActorId = 21, + MovieId = 6 + }, + new + { + ActorId = 22, + MovieId = 6 + }, + new + { + ActorId = 14, + MovieId = 7 + }, + new + { + ActorId = 23, + MovieId = 7 + }, + new + { + ActorId = 14, + MovieId = 8 + }, + new + { + ActorId = 24, + MovieId = 8 + }, + new + { + ActorId = 25, + MovieId = 8 + }, + new + { + ActorId = 23, + MovieId = 9 + }, + new + { + ActorId = 27, + MovieId = 9 + }, + new + { + ActorId = 23, + MovieId = 10 + }, + new + { + ActorId = 28, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Copies") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.HasOne("CodeFirst.Models.Models.Clients", "Client") + .WithMany("Rentals") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Copies", "Copy") + .WithMany("Rentals") + .HasForeignKey("CopyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Copy"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.HasOne("CodeFirst.Models.Models.Actors", "Actor") + .WithMany("Starring") + .HasForeignKey("ActorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Starring") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Actor"); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Navigation("Starring"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Navigation("Copies"); + + b.Navigation("Starring"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.cs b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.cs new file mode 100644 index 00000000..b2c0a7fc --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/20220124205405_SqlServerInitialMigration.cs @@ -0,0 +1,358 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace CodeFirst.DataAccess.Migrations.SqlServerDb; + +public partial class SqlServerInitialMigration : Migration +{ + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "actors", + columns: table => new + { + actor_id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + first_name = table.Column(type: "nvarchar(max)", nullable: true), + last_name = table.Column(type: "nvarchar(max)", nullable: true), + birthday = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("actor_pkey", x => x.actor_id); + }); + + migrationBuilder.CreateTable( + name: "clients", + columns: table => new + { + client_id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + first_name = table.Column(type: "nvarchar(max)", nullable: true), + last_name = table.Column(type: "nvarchar(max)", nullable: true), + birthday = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("client_pkey", x => x.client_id); + }); + + migrationBuilder.CreateTable( + name: "employees", + columns: table => new + { + employee_id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + first_name = table.Column(type: "nvarchar(max)", nullable: false), + last_name = table.Column(type: "nvarchar(max)", nullable: false), + salary = table.Column(type: "real", nullable: true), + City = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("employee_id", x => x.employee_id); + }); + + migrationBuilder.CreateTable( + name: "movies", + columns: table => new + { + movie_id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + title = table.Column(type: "nvarchar(max)", nullable: true), + year = table.Column(type: "int", nullable: false), + age_restriction = table.Column(type: "int", nullable: false), + price = table.Column(type: "real", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("movies_pkey", x => x.movie_id); + }); + + migrationBuilder.CreateTable( + name: "copies", + columns: table => new + { + copy_id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + available = table.Column(type: "bit", nullable: false), + movie_id = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("copies_pkey", x => x.copy_id); + table.ForeignKey( + name: "FK_copies_movies_movie_id", + column: x => x.movie_id, + principalTable: "movies", + principalColumn: "movie_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "starring", + columns: table => new + { + actor_id = table.Column(type: "int", nullable: false), + movie_id = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_starring", x => new { x.actor_id, x.movie_id }); + table.ForeignKey( + name: "FK_starring_actors_actor_id", + column: x => x.actor_id, + principalTable: "actors", + principalColumn: "actor_id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_starring_movies_movie_id", + column: x => x.movie_id, + principalTable: "movies", + principalColumn: "movie_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "rentals", + columns: table => new + { + copy_id = table.Column(type: "int", nullable: false), + client_id = table.Column(type: "int", nullable: false), + date_of_rental = table.Column(type: "datetime2", nullable: true), + date_of_return = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("rentals_pkey", x => new { x.client_id, x.copy_id }); + table.ForeignKey( + name: "FK_rentals_clients_client_id", + column: x => x.client_id, + principalTable: "clients", + principalColumn: "client_id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_rentals_copies_copy_id", + column: x => x.copy_id, + principalTable: "copies", + principalColumn: "copy_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.InsertData( + table: "actors", + columns: new[] { "actor_id", "birthday", "first_name", "last_name" }, + values: new object[,] + { + { 1, new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Arnold", "Schwarzenegger" }, + { 28, new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), "Tom", "Cruise" }, + { 27, new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), "Natalie", "Portman" }, + { 26, new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Gary", "Oldman" }, + { 24, new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), "Billy", "Crystal" }, + { 23, new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Jean", "Reno" }, + { 22, new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), "Emmanuelle", "Seigner" }, + { 21, new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harrison", "Ford" }, + { 20, new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), "Charlie", "Sheen" }, + { 19, new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), "Willem", "Dafoe" }, + { 18, new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), "Tom", "Berenger" }, + { 17, new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), "Cybill", "Shepherd" }, + { 16, new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harvey", "Keitel" }, + { 15, new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "Jodie", "Foster" }, + { 25, new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), "Lisa", "Kudrow" }, + { 13, new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), "Sigourney", "Weaver" }, + { 12, new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Dan", "Aykroyd" }, + { 11, new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bill", "Murray" }, + { 10, new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), "Linda", "Hamilton" }, + { 9, new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), "Michael", "Biehn" }, + { 8, new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), "Peter", "Mayhew" }, + { 7, new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "David", "Prowse" }, + { 6, new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), "Peter", "Cushing" }, + { 5, new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), "Alec", "Guiness" }, + { 4, new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Carrie", "Fisher" }, + { 3, new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "Harrison", "Ford" }, + { 2, new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), "Anthony", "Daniels" }, + { 14, new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), "Robert", "De Niro" } + }); + + migrationBuilder.InsertData( + table: "clients", + columns: new[] { "client_id", "birthday", "first_name", "last_name" }, + values: new object[,] + { + { 6, new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), "Rick", "Sanchez" }, + { 5, new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), "Lisa", "Simpson" }, + { 4, new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), "Bob", "Belcher" }, + { 2, new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), "Brian", "Griffin" }, + { 1, new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), "Hank", "Hill" }, + { 3, new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), "Gary", "Goodspeed" } + }); + + migrationBuilder.InsertData( + table: "employees", + columns: new[] { "employee_id", "City", "first_name", "last_name", "salary" }, + values: new object[,] + { + { 1, "New York", "John", "Smith", 150f }, + { 2, "New York", "Ben", "Johnson", 250f }, + { 3, "New Orleans", "Louis", "Armstrong", 75f }, + { 4, "London", "John", "Lennon", 300f }, + { 5, "London", "Peter", "Gabriel", 150f } + }); + + migrationBuilder.InsertData( + table: "movies", + columns: new[] { "movie_id", "age_restriction", "price", "title", "year" }, + values: new object[,] + { + { 8, 16, 10.5f, "Analyze This", 1999 }, + { 7, 13, 9.5f, "Ronin", 1998 }, + { 6, 15, 8.5f, "Frantic", 1988 } + }); + + migrationBuilder.InsertData( + table: "movies", + columns: new[] { "movie_id", "age_restriction", "price", "title", "year" }, + values: new object[,] + { + { 5, 18, 5f, "Platoon", 1986 }, + { 1, 12, 10f, "Star Wars Episode IV: A New Hope", 1979 }, + { 3, 15, 8.5f, "Terminator", 1984 }, + { 2, 12, 5.5f, "Ghostbusters", 1984 }, + { 9, 16, 8.5f, "Leon: the Professional", 1994 }, + { 4, 17, 5f, "Taxi Driver", 1976 }, + { 10, 13, 8.5f, "Mission Impossible", 1996 } + }); + + migrationBuilder.InsertData( + table: "copies", + columns: new[] { "copy_id", "available", "movie_id" }, + values: new object[,] + { + { 1, true, 1 }, + { 11, true, 6 }, + { 8, false, 5 }, + { 12, true, 7 }, + { 13, true, 7 }, + { 7, true, 4 }, + { 14, false, 8 }, + { 6, true, 3 }, + { 10, false, 6 }, + { 4, true, 3 }, + { 5, false, 3 }, + { 17, false, 10 }, + { 2, false, 1 }, + { 20, true, 10 }, + { 19, true, 10 }, + { 18, true, 10 }, + { 15, true, 9 }, + { 9, true, 6 }, + { 3, true, 2 }, + { 16, true, 10 } + }); + + migrationBuilder.InsertData( + table: "starring", + columns: new[] { "actor_id", "movie_id" }, + values: new object[,] + { + { 23, 7 }, + { 14, 8 }, + { 14, 7 }, + { 27, 9 }, + { 23, 9 }, + { 22, 6 }, + { 21, 6 }, + { 24, 8 }, + { 25, 8 }, + { 18, 5 }, + { 19, 5 }, + { 2, 1 }, + { 3, 1 }, + { 4, 1 }, + { 5, 1 }, + { 6, 1 }, + { 7, 1 }, + { 8, 1 }, + { 11, 2 }, + { 20, 5 }, + { 12, 2 }, + { 1, 3 } + }); + + migrationBuilder.InsertData( + table: "starring", + columns: new[] { "actor_id", "movie_id" }, + values: new object[,] + { + { 9, 3 }, + { 10, 3 }, + { 14, 4 }, + { 15, 4 }, + { 16, 4 }, + { 17, 4 }, + { 23, 10 }, + { 13, 2 }, + { 28, 10 } + }); + + migrationBuilder.InsertData( + table: "rentals", + columns: new[] { "client_id", "copy_id", "date_of_rental", "date_of_return" }, + values: new object[,] + { + { 1, 1, new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 1, new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 3, new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 3, new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 5, new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 1, 6, new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 2, 7, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 7, new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 7, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 5, 11, new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 12, new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 4, 13, new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 6, 19, new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) }, + { 3, 20, new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) } + }); + + migrationBuilder.CreateIndex( + name: "IX_copies_movie_id", + table: "copies", + column: "movie_id"); + + migrationBuilder.CreateIndex( + name: "IX_rentals_copy_id", + table: "rentals", + column: "copy_id"); + + migrationBuilder.CreateIndex( + name: "IX_starring_movie_id", + table: "starring", + column: "movie_id"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "employees"); + + migrationBuilder.DropTable( + name: "rentals"); + + migrationBuilder.DropTable( + name: "starring"); + + migrationBuilder.DropTable( + name: "clients"); + + migrationBuilder.DropTable( + name: "copies"); + + migrationBuilder.DropTable( + name: "actors"); + + migrationBuilder.DropTable( + name: "movies"); + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/SqlServerDbContextModelSnapshot.cs b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/SqlServerDbContextModelSnapshot.cs new file mode 100644 index 00000000..c00d00c7 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Migrations/SqlServerDb/SqlServerDbContextModelSnapshot.cs @@ -0,0 +1,1020 @@ +// +using System; +using CodeFirst.DataAccess.Context; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace CodeFirst.DataAccess.Migrations.SqlServerDb +{ + [DbContext(typeof(SqlServerDbContext))] + partial class SqlServerDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Property("ActorId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("actor_id") + .UseIdentityColumn(); + + b.Property("Birthday") + .HasColumnType("datetime2") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.HasKey("ActorId") + .HasName("actor_pkey"); + + b.ToTable("actors"); + + b.HasData( + new + { + ActorId = 1, + Birthday = new DateTime(1947, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Arnold", + Lastname = "Schwarzenegger" + }, + new + { + ActorId = 2, + Birthday = new DateTime(1946, 2, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Anthony", + Lastname = "Daniels" + }, + new + { + ActorId = 3, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 4, + Birthday = new DateTime(1956, 10, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Carrie", + Lastname = "Fisher" + }, + new + { + ActorId = 5, + Birthday = new DateTime(1914, 4, 2, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Alec", + Lastname = "Guiness" + }, + new + { + ActorId = 6, + Birthday = new DateTime(1913, 5, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Cushing" + }, + new + { + ActorId = 7, + Birthday = new DateTime(1944, 5, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "David", + Lastname = "Prowse" + }, + new + { + ActorId = 8, + Birthday = new DateTime(1935, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Peter", + Lastname = "Mayhew" + }, + new + { + ActorId = 9, + Birthday = new DateTime(1956, 7, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Michael", + Lastname = "Biehn" + }, + new + { + ActorId = 10, + Birthday = new DateTime(1956, 9, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Linda", + Lastname = "Hamilton" + }, + new + { + ActorId = 11, + Birthday = new DateTime(1950, 9, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bill", + Lastname = "Murray" + }, + new + { + ActorId = 12, + Birthday = new DateTime(1952, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Dan", + Lastname = "Aykroyd" + }, + new + { + ActorId = 13, + Birthday = new DateTime(1949, 10, 8, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Sigourney", + Lastname = "Weaver" + }, + new + { + ActorId = 14, + Birthday = new DateTime(1943, 8, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Robert", + Lastname = "De Niro" + }, + new + { + ActorId = 15, + Birthday = new DateTime(1962, 11, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jodie", + Lastname = "Foster" + }, + new + { + ActorId = 16, + Birthday = new DateTime(1939, 5, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harvey", + Lastname = "Keitel" + }, + new + { + ActorId = 17, + Birthday = new DateTime(1950, 2, 18, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Cybill", + Lastname = "Shepherd" + }, + new + { + ActorId = 18, + Birthday = new DateTime(1949, 5, 31, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Berenger" + }, + new + { + ActorId = 19, + Birthday = new DateTime(1955, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Willem", + Lastname = "Dafoe" + }, + new + { + ActorId = 20, + Birthday = new DateTime(1965, 9, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Charlie", + Lastname = "Sheen" + }, + new + { + ActorId = 21, + Birthday = new DateTime(1942, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Harrison", + Lastname = "Ford" + }, + new + { + ActorId = 22, + Birthday = new DateTime(1966, 6, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Emmanuelle", + Lastname = "Seigner" + }, + new + { + ActorId = 23, + Birthday = new DateTime(1948, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Jean", + Lastname = "Reno" + }, + new + { + ActorId = 24, + Birthday = new DateTime(1948, 3, 14, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Billy", + Lastname = "Crystal" + }, + new + { + ActorId = 25, + Birthday = new DateTime(1963, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Kudrow" + }, + new + { + ActorId = 26, + Birthday = new DateTime(1958, 3, 21, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Oldman" + }, + new + { + ActorId = 27, + Birthday = new DateTime(1981, 6, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Natalie", + Lastname = "Portman" + }, + new + { + ActorId = 28, + Birthday = new DateTime(1962, 7, 3, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Tom", + Lastname = "Cruise" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Property("ClientId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("client_id") + .UseIdentityColumn(); + + b.Property("Birthday") + .HasColumnType("datetime2") + .HasColumnName("birthday"); + + b.Property("Firstname") + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.HasKey("ClientId") + .HasName("client_pkey"); + + b.ToTable("clients"); + + b.HasData( + new + { + ClientId = 1, + Birthday = new DateTime(1954, 4, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Hank", + Lastname = "Hill" + }, + new + { + ClientId = 2, + Birthday = new DateTime(2011, 9, 11, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Brian", + Lastname = "Griffin" + }, + new + { + ClientId = 3, + Birthday = new DateTime(1989, 3, 12, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Gary", + Lastname = "Goodspeed" + }, + new + { + ClientId = 4, + Birthday = new DateTime(1977, 1, 23, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Bob", + Lastname = "Belcher" + }, + new + { + ClientId = 5, + Birthday = new DateTime(2012, 5, 9, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Lisa", + Lastname = "Simpson" + }, + new + { + ClientId = 6, + Birthday = new DateTime(1965, 3, 17, 0, 0, 0, 0, DateTimeKind.Unspecified), + Firstname = "Rick", + Lastname = "Sanchez" + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Property("CopyId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("copy_id") + .UseIdentityColumn(); + + b.Property("Available") + .HasColumnType("bit") + .HasColumnName("available"); + + b.Property("MovieId") + .HasColumnType("int") + .HasColumnName("movie_id"); + + b.HasKey("CopyId") + .HasName("copies_pkey"); + + b.HasIndex("MovieId"); + + b.ToTable("copies"); + + b.HasData( + new + { + CopyId = 1, + Available = true, + MovieId = 1 + }, + new + { + CopyId = 2, + Available = false, + MovieId = 1 + }, + new + { + CopyId = 3, + Available = true, + MovieId = 2 + }, + new + { + CopyId = 4, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 5, + Available = false, + MovieId = 3 + }, + new + { + CopyId = 6, + Available = true, + MovieId = 3 + }, + new + { + CopyId = 7, + Available = true, + MovieId = 4 + }, + new + { + CopyId = 8, + Available = false, + MovieId = 5 + }, + new + { + CopyId = 9, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 10, + Available = false, + MovieId = 6 + }, + new + { + CopyId = 11, + Available = true, + MovieId = 6 + }, + new + { + CopyId = 12, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 13, + Available = true, + MovieId = 7 + }, + new + { + CopyId = 14, + Available = false, + MovieId = 8 + }, + new + { + CopyId = 15, + Available = true, + MovieId = 9 + }, + new + { + CopyId = 16, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 17, + Available = false, + MovieId = 10 + }, + new + { + CopyId = 18, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 19, + Available = true, + MovieId = 10 + }, + new + { + CopyId = 20, + Available = true, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Employees", b => + { + b.Property("EmployeeId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("employee_id") + .UseIdentityColumn(); + + b.Property("City") + .HasColumnType("nvarchar(max)"); + + b.Property("Firstname") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("first_name"); + + b.Property("Lastname") + .IsRequired() + .HasColumnType("nvarchar(max)") + .HasColumnName("last_name"); + + b.Property("Salary") + .HasColumnType("real") + .HasColumnName("salary"); + + b.HasKey("EmployeeId") + .HasName("employee_id"); + + b.ToTable("employees"); + + b.HasData( + new + { + EmployeeId = 1, + City = "New York", + Firstname = "John", + Lastname = "Smith", + Salary = 150f + }, + new + { + EmployeeId = 2, + City = "New York", + Firstname = "Ben", + Lastname = "Johnson", + Salary = 250f + }, + new + { + EmployeeId = 3, + City = "New Orleans", + Firstname = "Louis", + Lastname = "Armstrong", + Salary = 75f + }, + new + { + EmployeeId = 4, + City = "London", + Firstname = "John", + Lastname = "Lennon", + Salary = 300f + }, + new + { + EmployeeId = 5, + City = "London", + Firstname = "Peter", + Lastname = "Gabriel", + Salary = 150f + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Property("MovieId") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasColumnName("movie_id") + .UseIdentityColumn(); + + b.Property("AgeRestriction") + .HasColumnType("int") + .HasColumnName("age_restriction"); + + b.Property("Price") + .HasColumnType("real") + .HasColumnName("price"); + + b.Property("Title") + .HasColumnType("nvarchar(max)") + .HasColumnName("title"); + + b.Property("Year") + .HasColumnType("int") + .HasColumnName("year"); + + b.HasKey("MovieId") + .HasName("movies_pkey"); + + b.ToTable("movies"); + + b.HasData( + new + { + MovieId = 1, + AgeRestriction = 12, + Price = 10f, + Title = "Star Wars Episode IV: A New Hope", + Year = 1979 + }, + new + { + MovieId = 2, + AgeRestriction = 12, + Price = 5.5f, + Title = "Ghostbusters", + Year = 1984 + }, + new + { + MovieId = 3, + AgeRestriction = 15, + Price = 8.5f, + Title = "Terminator", + Year = 1984 + }, + new + { + MovieId = 4, + AgeRestriction = 17, + Price = 5f, + Title = "Taxi Driver", + Year = 1976 + }, + new + { + MovieId = 5, + AgeRestriction = 18, + Price = 5f, + Title = "Platoon", + Year = 1986 + }, + new + { + MovieId = 6, + AgeRestriction = 15, + Price = 8.5f, + Title = "Frantic", + Year = 1988 + }, + new + { + MovieId = 7, + AgeRestriction = 13, + Price = 9.5f, + Title = "Ronin", + Year = 1998 + }, + new + { + MovieId = 8, + AgeRestriction = 16, + Price = 10.5f, + Title = "Analyze This", + Year = 1999 + }, + new + { + MovieId = 9, + AgeRestriction = 16, + Price = 8.5f, + Title = "Leon: the Professional", + Year = 1994 + }, + new + { + MovieId = 10, + AgeRestriction = 13, + Price = 8.5f, + Title = "Mission Impossible", + Year = 1996 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.Property("ClientId") + .HasColumnType("int") + .HasColumnName("client_id"); + + b.Property("CopyId") + .HasColumnType("int") + .HasColumnName("copy_id"); + + b.Property("DateOfRental") + .HasColumnType("datetime2") + .HasColumnName("date_of_rental"); + + b.Property("DateOfReturn") + .HasColumnType("datetime2") + .HasColumnName("date_of_return"); + + b.HasKey("ClientId", "CopyId") + .HasName("rentals_pkey"); + + b.HasIndex("CopyId"); + + b.ToTable("rentals"); + + b.HasData( + new + { + ClientId = 1, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 4, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 1, + CopyId = 6, + DateOfRental = new DateTime(2005, 7, 19, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 5, + DateOfRental = new DateTime(2005, 7, 26, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 27, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 2, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 12, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 20, + DateOfRental = new DateTime(2005, 7, 16, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 17, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 3, + DateOfRental = new DateTime(2005, 7, 22, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 23, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 3, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 24, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 25, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 4, + CopyId = 13, + DateOfRental = new DateTime(2005, 7, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 5, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 5, + CopyId = 11, + DateOfRental = new DateTime(2005, 7, 10, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 1, + DateOfRental = new DateTime(2005, 7, 6, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 7, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 7, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }, + new + { + ClientId = 6, + CopyId = 19, + DateOfRental = new DateTime(2005, 7, 29, 0, 0, 0, 0, DateTimeKind.Unspecified), + DateOfReturn = new DateTime(2005, 7, 30, 0, 0, 0, 0, DateTimeKind.Unspecified) + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.Property("ActorId") + .HasColumnType("int") + .HasColumnName("actor_id"); + + b.Property("MovieId") + .HasColumnType("int") + .HasColumnName("movie_id"); + + b.HasKey("ActorId", "MovieId"); + + b.HasIndex("MovieId"); + + b.ToTable("starring"); + + b.HasData( + new + { + ActorId = 2, + MovieId = 1 + }, + new + { + ActorId = 3, + MovieId = 1 + }, + new + { + ActorId = 4, + MovieId = 1 + }, + new + { + ActorId = 5, + MovieId = 1 + }, + new + { + ActorId = 6, + MovieId = 1 + }, + new + { + ActorId = 7, + MovieId = 1 + }, + new + { + ActorId = 8, + MovieId = 1 + }, + new + { + ActorId = 1, + MovieId = 3 + }, + new + { + ActorId = 9, + MovieId = 3 + }, + new + { + ActorId = 10, + MovieId = 3 + }, + new + { + ActorId = 11, + MovieId = 2 + }, + new + { + ActorId = 12, + MovieId = 2 + }, + new + { + ActorId = 13, + MovieId = 2 + }, + new + { + ActorId = 14, + MovieId = 4 + }, + new + { + ActorId = 15, + MovieId = 4 + }, + new + { + ActorId = 16, + MovieId = 4 + }, + new + { + ActorId = 17, + MovieId = 4 + }, + new + { + ActorId = 18, + MovieId = 5 + }, + new + { + ActorId = 19, + MovieId = 5 + }, + new + { + ActorId = 20, + MovieId = 5 + }, + new + { + ActorId = 21, + MovieId = 6 + }, + new + { + ActorId = 22, + MovieId = 6 + }, + new + { + ActorId = 14, + MovieId = 7 + }, + new + { + ActorId = 23, + MovieId = 7 + }, + new + { + ActorId = 14, + MovieId = 8 + }, + new + { + ActorId = 24, + MovieId = 8 + }, + new + { + ActorId = 25, + MovieId = 8 + }, + new + { + ActorId = 23, + MovieId = 9 + }, + new + { + ActorId = 27, + MovieId = 9 + }, + new + { + ActorId = 23, + MovieId = 10 + }, + new + { + ActorId = 28, + MovieId = 10 + }); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Copies") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Rentals", b => + { + b.HasOne("CodeFirst.Models.Models.Clients", "Client") + .WithMany("Rentals") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Copies", "Copy") + .WithMany("Rentals") + .HasForeignKey("CopyId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Copy"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Starring", b => + { + b.HasOne("CodeFirst.Models.Models.Actors", "Actor") + .WithMany("Starring") + .HasForeignKey("ActorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("CodeFirst.Models.Models.Movies", "Movie") + .WithMany("Starring") + .HasForeignKey("MovieId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Actor"); + + b.Navigation("Movie"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Actors", b => + { + b.Navigation("Starring"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Clients", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Copies", b => + { + b.Navigation("Rentals"); + }); + + modelBuilder.Entity("CodeFirst.Models.Models.Movies", b => + { + b.Navigation("Copies"); + + b.Navigation("Starring"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src-examples/CodeFirst.DataAccess/Models/Actors.cs b/src-examples/CodeFirst.DataAccess/Models/Actors.cs new file mode 100644 index 00000000..84655243 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Actors.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace CodeFirst.DataAccess.Models; + +public class Actors +{ + public int ActorId { get; set; } + public string Firstname { get; set; } + public string Lastname { get; set; } + public DateTime? Birthday { get; set; } + + public Actors() + { + } + + public Actors(int actorId, string firstname, string lastname, DateTime? birthday) + { + ActorId = actorId; + Firstname = firstname; + Lastname = lastname; + Birthday = birthday; + } + + // navigational properties + // this is composite key to get many to many movies - actors + public virtual ICollection Starring { get; set; } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Clients.cs b/src-examples/CodeFirst.DataAccess/Models/Clients.cs new file mode 100644 index 00000000..bedf4391 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Clients.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; + +namespace CodeFirst.DataAccess.Models; + +public class Clients +{ + public int ClientId { get; set; } + public string Firstname { get; set; } + public string Lastname { get; set; } + public DateTime? Birthday { get; set; } + + // navigational properties + public virtual ICollection Rentals { get; set; } + + public Clients() + { + } + + public Clients(int clientId, string firstname, string lastname, DateTime? birthday) + { + ClientId = clientId; + Firstname = firstname; + Lastname = lastname; + Birthday = birthday; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Copies.cs b/src-examples/CodeFirst.DataAccess/Models/Copies.cs new file mode 100644 index 00000000..cde3d0fe --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Copies.cs @@ -0,0 +1,28 @@ +using System.Collections.Generic; + +namespace CodeFirst.DataAccess.Models; + +public class Copies +{ + public int CopyId { get; set; } + public bool Available { get; set; } + public int MovieId { get; set; } + + // navigational properties + // one copy has only one movie + public virtual Movies Movie { get; set; } + + // to maintain many to many copies - rentals + public virtual ICollection Rentals { get; set; } + + public Copies() + { + } + + public Copies(int copyId, int movieId, bool available) + { + CopyId = copyId; + MovieId = movieId; + Available = available; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Employees.cs b/src-examples/CodeFirst.DataAccess/Models/Employees.cs new file mode 100644 index 00000000..259f282b --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Employees.cs @@ -0,0 +1,23 @@ +namespace CodeFirst.DataAccess.Models; + +public class Employees +{ + public int EmployeeId { get; set; } + public string Firstname { get; set; } + public string Lastname { get; set; } + public float? Salary { get; set; } // yeah, salary might be nullable, why not :) + public string City { get; set; } + + public Employees() + { + } + + public Employees(int employeeId, string firstname, string lastname, float? salary, string city) + { + EmployeeId = employeeId; + Firstname = firstname; + Lastname = lastname; + Salary = salary; + City = city; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Movies.cs b/src-examples/CodeFirst.DataAccess/Models/Movies.cs new file mode 100644 index 00000000..ad007527 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Movies.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; + +namespace CodeFirst.DataAccess.Models; + +public class Movies +{ + public int MovieId { get; set; } + public string Title { get; set; } + public int Year { get; set; } + public int AgeRestriction { get; set; } + public float Price { get; set; } + + public Movies() + { + } + + public Movies(int movieId, string title, int year, int ageRestriction, float price) + { + MovieId = movieId; + Title = title; + Year = year; + AgeRestriction = ageRestriction; + Price = price; + } + + // navigational properties + // one movie may have many copies + public virtual ICollection Copies { get; set; } + // this is composite key to get many to many movies - actors + public virtual ICollection Starring { get; set; } + + public override string ToString() + { + return $"{MovieId}, {Title}, {Year}, {Price}"; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Rentals.cs b/src-examples/CodeFirst.DataAccess/Models/Rentals.cs new file mode 100644 index 00000000..d308abca --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Rentals.cs @@ -0,0 +1,27 @@ +using System; + +namespace CodeFirst.DataAccess.Models; + +public class Rentals +{ + public int CopyId { get; set; } + public int ClientId { get; set; } + public DateTime? DateOfRental { get; set; } + public DateTime? DateOfReturn { get; set; } + + // navigational properties + public virtual Clients Client { get; set; } + public virtual Copies Copy { get; set; } + + public Rentals() + { + } + + public Rentals(int clientId, int copyId, DateTime? dateOfRental, DateTime? dateOfReturn) + { + CopyId = copyId; + ClientId = clientId; + DateOfRental = dateOfRental; + DateOfReturn = dateOfReturn; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Models/Starring.cs b/src-examples/CodeFirst.DataAccess/Models/Starring.cs new file mode 100644 index 00000000..0a876417 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Models/Starring.cs @@ -0,0 +1,21 @@ +namespace CodeFirst.DataAccess.Models; + +public class Starring +{ + public int ActorId { get; set; } + public int MovieId { get; set; } + + // navigational properties + public virtual Movies Movie { get; set; } + public virtual Actors Actor { get; set; } + + public Starring() + { + } + + public Starring(int actorId, int movieId) + { + ActorId = actorId; + MovieId = movieId; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/ActorsRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/ActorsRepository.cs new file mode 100644 index 00000000..d11a9e3e --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/ActorsRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class ActorsRepository : BaseRepository +{ + public ActorsRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/BaseRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/BaseRepository.cs new file mode 100644 index 00000000..6164be8b --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/BaseRepository.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using CodeFirst.DataAccess.Interfaces; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public abstract class BaseRepository : IRepository where T : class +{ + private readonly DbContext _baseContext; + private readonly DbSet _dbSet; + + protected BaseRepository(DbContext baseContext) + { + _baseContext = baseContext; + _dbSet = _baseContext.Set(); + } + + public async Task AddAsync(T entity) + { + await _dbSet.AddAsync(entity); + } + + public void Update(T entity) + { + _dbSet.Attach(entity); + _baseContext.Entry(entity).State = EntityState.Modified; + } + + public void Delete(T entity) + { + _dbSet.Remove(entity); + } + + public void Delete(Expression> where) + { + var objects = _dbSet.Where(where).AsEnumerable(); + foreach (var obj in objects) + _dbSet.Remove(obj); + } + + public async Task GetByIdAsync(int id) + { + return await _dbSet.FindAsync(id); + } + + public async Task GetSingleAsync(Expression> where) + { + return await _dbSet.FirstOrDefaultAsync(where); + } + + public async Task> GetAllAsync() + { + return await _dbSet.ToListAsync(); + } + + public async Task> GetManyAsync(Expression> where) + { + return await _dbSet.Where(where).ToListAsync(); + } + + public async Task SaveChangesAsync() + { + return await _baseContext.SaveChangesAsync() > 0; + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/ClientsRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/ClientsRepository.cs new file mode 100644 index 00000000..d0ec20e7 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/ClientsRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class ClientsRepository : BaseRepository +{ + public ClientsRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/CopiesRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/CopiesRepository.cs new file mode 100644 index 00000000..6e0ed256 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/CopiesRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class CopiesRepository : BaseRepository +{ + public CopiesRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/EmployeesRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/EmployeesRepository.cs new file mode 100644 index 00000000..4ef4a5aa --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/EmployeesRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class EmployeesRepository : BaseRepository +{ + public EmployeesRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/MoviesRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/MoviesRepository.cs new file mode 100644 index 00000000..834db90a --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/MoviesRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class MoviesRepository : BaseRepository +{ + public MoviesRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/RentalsRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/RentalsRepository.cs new file mode 100644 index 00000000..ed1b7dd3 --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/RentalsRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class RentalsRepository : BaseRepository +{ + public RentalsRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/CodeFirst.DataAccess/Repositories/StarringRepository.cs b/src-examples/CodeFirst.DataAccess/Repositories/StarringRepository.cs new file mode 100644 index 00000000..687f72df --- /dev/null +++ b/src-examples/CodeFirst.DataAccess/Repositories/StarringRepository.cs @@ -0,0 +1,11 @@ +using CodeFirst.DataAccess.Models; +using Microsoft.EntityFrameworkCore; + +namespace CodeFirst.DataAccess.Repositories; + +public class StarringRepository : BaseRepository +{ + public StarringRepository(DbContext baseContext) : base(baseContext) + { + } +} \ No newline at end of file diff --git a/src-examples/README.md b/src-examples/README.md new file mode 100644 index 00000000..c721d04e --- /dev/null +++ b/src-examples/README.md @@ -0,0 +1,76 @@ +# Code First with Entity Framework + +Simple example of Entity Framework Core Code-First approach. + +## Required packages and settings + +Nuget Packages: +- `Microsoft.EntityFrameworkCore` +- `Microsoft.EntityFrameworkCore.Tools` +- `Microsoft.EntityFrameworkCore.SqlServer` +- `Npgsql.EntityFrameworkCore.PostgreSQL` + +Settings: +- Environment variable: `SQLSERVER_MOVIES_LOCAL_CONNSTR` +- Environment variable: `POSTGRES_MOVIES_LOCAL_CONNSTR` + +## Entity Framework CLI commands + +Postgres: +- `dotnet-ef database update --project src-examples\CodeFirst.DataAccess --context PostgresDbContext` +- `dotnet-ef database drop --project src-examples\CodeFirst.DataAccess --context PostgresDbContext` + +MS SQL Server: +- `dotnet-ef database update --project src-examples\CodeFirst.DataAccess --context SqlServerDbContext` +- `dotnet-ef database drop --project src-examples\CodeFirst.DataAccess --context PostgresDbContext` + +## How to code first? + +- Set proper environmental variable for connection string for SQL Server database. For localhost it is: + + `"Data Source=DESKTOP-RN0NICT;Initial Catalog=MoviesCodeFirst;Integrated Security=true;"` + +- Set proper environmental variable for connection string for Postgre SQL database. For localhost it is: + + `"Server=localhost;User Id=postgres;Password=postgres;Database=MoviesCodeFirst;"` + +- Build a relations between models, for instance, `one-to-one`, `one-to-many`, `many-to-many` according to the database diagram. + Refer to + - https://www.learnentityframeworkcore.com/configuration/one-to-one-relationship-configuration + - https://www.learnentityframeworkcore.com/relationships/managing-one-to-many-relationships + - https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration + +- Create proper configurations of models, using Fluent API. Configuration should implement `IEntityTypeConfiguration` interface from Entity Framework namespace +- Create `BaseContext` inherrit it from `DbContext` in Entity Framework namespace, add related sets `BDSet`, where `T` -- models. +- Create `SqlServerContext`, inherrit it from `BaseContext` and override methods `OnConfiguring` and `OnModelCreating`. +- Create `PostgreSqlContext`, inherrit it from `BaseContext` and override methods `OnConfiguring` and `OnModelCreating`. +- Create migration for `SqlServerContext`, use CLI command: + + `dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContext` + +- Create migration for `PostgreSqlContext`, use CLI command: + + `dotnet-ef migrations add PostgreSqlInitialMigration --project CodeFirst.DataAccess --context PostgresDbContext` + +- Update SQL Server database, use CLI command: + + `dotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContext` + +- Update Postgre SQL database, use CLI command: + + `dotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContext` + +- Write unit tests for `SqlServerDbContext` +- Write unit tests for `PostgresDbContext` +- Create Generic repository interface +- Implement Generic repository interface +- Test Generic Repositories + +## Notes + +To get MS SQL connection string proceed: +- Go to visual studio - Server explorer - Data connections +- Right click on data connections - Add new connection +- Type there your server name +- Click add connection +- Then in properties of such connection you'd get connection string \ No newline at end of file