├── README.md ├── .gitignore └── EFCoreCodeFirstSample ├── EFCoreCodeFirstSample ├── appsettings.Development.json ├── appsettings.json ├── Models │ ├── Repository │ │ └── IDataRepository.cs │ ├── Employee.cs │ ├── EmployeeContext.cs │ └── DataManager │ │ └── EmployeeManager.cs ├── Migrations │ ├── 20191024081309_EFCoreCodeFirstSample.Models.AddEmployeeGender.cs │ ├── 20191024081010_EFCoreCodeFirstSample.Models.EmployeeContext.cs │ ├── 20191024081149_EFCoreCodeFirstSample.Models.EmployeeContextSeed.cs │ ├── 20191024081010_EFCoreCodeFirstSample.Models.EmployeeContext.Designer.cs │ ├── EmployeeContextModelSnapshot.cs │ ├── 20191024081149_EFCoreCodeFirstSample.Models.EmployeeContextSeed.Designer.cs │ └── 20191024081309_EFCoreCodeFirstSample.Models.AddEmployeeGender.Designer.cs ├── Program.cs ├── EFCoreCodeFirstSample.csproj ├── Properties │ └── launchSettings.json ├── EFCoreCodeFirstSample.csproj.user ├── Startup.cs └── Controllers │ └── EmployeeController.cs └── EFCoreCodeFirstSample.sln /README.md: -------------------------------------------------------------------------------- 1 | # .NET Core Web API with EF Core Code-First approach 2 | ## https://code-maze.com/net-core-web-api-ef-core-code-first 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /EFCoreCodeFirstSample/.vs 2 | /EFCoreCodeFirstSample/EFCoreCodeFirstSample/obj 3 | /EFCoreCodeFirstSample/EFCoreCodeFirstSample/bin 4 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "ConnectionString": { 10 | "EmployeeDB": "server=.;database=EmployeeDB;Integrated Security=true;" 11 | }, 12 | "AllowedHosts": "*" 13 | } 14 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Models/Repository/IDataRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace EFCoreCodeFirstSample.Models.Repository 4 | { 5 | public interface IDataRepository 6 | { 7 | IEnumerable GetAll(); 8 | TEntity Get(long id); 9 | void Add(TEntity entity); 10 | void Update(Employee employee, TEntity entity); 11 | void Delete(Employee employee); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Models/Employee.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EFCoreCodeFirstSample.Models 6 | { 7 | public class Employee 8 | { 9 | [Key] 10 | [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public long EmployeeId { get; set; } 12 | 13 | public string FirstName { get; set; } 14 | 15 | public string LastName { get; set; } 16 | 17 | public DateTime DateOfBirth { get; set; } 18 | 19 | public string PhoneNumber { get; set; } 20 | 21 | public string Email { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081309_EFCoreCodeFirstSample.Models.AddEmployeeGender.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace EFCoreCodeFirstSample.Migrations 4 | { 5 | public partial class EFCoreCodeFirstSampleModelsAddEmployeeGender : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.AddColumn( 10 | name: "Gender", 11 | table: "Employees", 12 | nullable: true); 13 | } 14 | 15 | protected override void Down(MigrationBuilder migrationBuilder) 16 | { 17 | migrationBuilder.DropColumn( 18 | name: "Gender", 19 | table: "Employees"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace EFCoreCodeFirstSample 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/EFCoreCodeFirstSample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | all 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:61976", 8 | "sslPort": 44362 9 | } 10 | }, 11 | "profiles": { 12 | "IIS Express": { 13 | "commandName": "IISExpress", 14 | "launchBrowser": false, 15 | "launchUrl": "api/values", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "EFCoreCodeFirstSample": { 21 | "commandName": "Project", 22 | "launchBrowser": false, 23 | "launchUrl": "api/values", 24 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/EFCoreCodeFirstSample.csproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ApiControllerEmptyScaffolder 5 | root/Controller 6 | 600 7 | True 8 | False 9 | True 10 | 11 | False 12 | 13 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29418.71 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreCodeFirstSample", "EFCoreCodeFirstSample\EFCoreCodeFirstSample.csproj", "{45314582-49B4-4E5D-AA4C-D9A24F3AEC60}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {45314582-49B4-4E5D-AA4C-D9A24F3AEC60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {45314582-49B4-4E5D-AA4C-D9A24F3AEC60}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {45314582-49B4-4E5D-AA4C-D9A24F3AEC60}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {45314582-49B4-4E5D-AA4C-D9A24F3AEC60}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {98DA9E70-6BDF-4D7E-AE5D-CB13D9C2AE85} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Models/EmployeeContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | 4 | namespace EFCoreCodeFirstSample.Models 5 | { 6 | public class EmployeeContext : DbContext 7 | { 8 | public EmployeeContext(DbContextOptions options) 9 | : base(options) 10 | { 11 | } 12 | 13 | public DbSet Employees { get; set; } 14 | 15 | protected override void OnModelCreating(ModelBuilder modelBuilder) 16 | { 17 | modelBuilder.Entity().HasData(new Employee 18 | { 19 | EmployeeId = 1, 20 | FirstName = "Uncle", 21 | LastName = "Bob", 22 | Email = "uncle.bob@gmail.com", 23 | DateOfBirth = new DateTime(1979, 04, 25), 24 | PhoneNumber = "999-888-7777" 25 | 26 | }, new Employee 27 | { 28 | EmployeeId = 2, 29 | FirstName = "Jan", 30 | LastName = "Kirsten", 31 | Email = "jan.kirsten@gmail.com", 32 | DateOfBirth = new DateTime(1981, 07, 13), 33 | PhoneNumber = "111-222-3333" 34 | }); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081010_EFCoreCodeFirstSample.Models.EmployeeContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace EFCoreCodeFirstSample.Migrations 5 | { 6 | public partial class EFCoreCodeFirstSampleModelsEmployeeContext : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Employees", 12 | columns: table => new 13 | { 14 | EmployeeId = table.Column(nullable: false) 15 | .Annotation("SqlServer:Identity", "1, 1"), 16 | FirstName = table.Column(nullable: true), 17 | LastName = table.Column(nullable: true), 18 | DateOfBirth = table.Column(nullable: false), 19 | PhoneNumber = table.Column(nullable: true), 20 | Email = table.Column(nullable: true) 21 | }, 22 | constraints: table => 23 | { 24 | table.PrimaryKey("PK_Employees", x => x.EmployeeId); 25 | }); 26 | } 27 | 28 | protected override void Down(MigrationBuilder migrationBuilder) 29 | { 30 | migrationBuilder.DropTable( 31 | name: "Employees"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081149_EFCoreCodeFirstSample.Models.EmployeeContextSeed.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace EFCoreCodeFirstSample.Migrations 5 | { 6 | public partial class EFCoreCodeFirstSampleModelsEmployeeContextSeed : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.InsertData( 11 | table: "Employees", 12 | columns: new[] { "EmployeeId", "DateOfBirth", "Email", "FirstName", "LastName", "PhoneNumber" }, 13 | values: new object[] { 1L, new DateTime(1979, 4, 25, 0, 0, 0, 0, DateTimeKind.Unspecified), "uncle.bob@gmail.com", "Uncle", "Bob", "999-888-7777" }); 14 | 15 | migrationBuilder.InsertData( 16 | table: "Employees", 17 | columns: new[] { "EmployeeId", "DateOfBirth", "Email", "FirstName", "LastName", "PhoneNumber" }, 18 | values: new object[] { 2L, new DateTime(1981, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), "jan.kirsten@gmail.com", "Jan", "Kirsten", "111-222-3333" }); 19 | } 20 | 21 | protected override void Down(MigrationBuilder migrationBuilder) 22 | { 23 | migrationBuilder.DeleteData( 24 | table: "Employees", 25 | keyColumn: "EmployeeId", 26 | keyValue: 1L); 27 | 28 | migrationBuilder.DeleteData( 29 | table: "Employees", 30 | keyColumn: "EmployeeId", 31 | keyValue: 2L); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Models/DataManager/EmployeeManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using EFCoreCodeFirstSample.Models.Repository; 4 | 5 | namespace EFCoreCodeFirstSample.Models.DataManager 6 | { 7 | public class EmployeeManager : IDataRepository 8 | { 9 | readonly EmployeeContext _employeeContext; 10 | 11 | public EmployeeManager(EmployeeContext context) 12 | { 13 | _employeeContext = context; 14 | } 15 | 16 | public IEnumerable GetAll() 17 | { 18 | return _employeeContext.Employees.ToList(); 19 | } 20 | 21 | public Employee Get(long id) 22 | { 23 | return _employeeContext.Employees.FirstOrDefault(e => e.EmployeeId == id); 24 | } 25 | 26 | public void Add(Employee entity) 27 | { 28 | _employeeContext.Employees.Add(entity); 29 | _employeeContext.SaveChanges(); 30 | } 31 | 32 | public void Update(Employee employee, Employee entity) 33 | { 34 | employee.FirstName = entity.FirstName; 35 | employee.LastName = entity.LastName; 36 | employee.Email = entity.Email; 37 | employee.DateOfBirth = entity.DateOfBirth; 38 | employee.PhoneNumber = entity.PhoneNumber; 39 | 40 | _employeeContext.SaveChanges(); 41 | } 42 | 43 | public void Delete(Employee employee) 44 | { 45 | _employeeContext.Employees.Remove(employee); 46 | _employeeContext.SaveChanges(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Startup.cs: -------------------------------------------------------------------------------- 1 | using EFCoreCodeFirstSample.Models; 2 | using EFCoreCodeFirstSample.Models.DataManager; 3 | using EFCoreCodeFirstSample.Models.Repository; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.EntityFrameworkCore; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Hosting; 10 | 11 | namespace EFCoreCodeFirstSample 12 | { 13 | public class Startup 14 | { 15 | public Startup(IConfiguration configuration) 16 | { 17 | Configuration = configuration; 18 | } 19 | 20 | public IConfiguration Configuration { get; } 21 | 22 | // This method gets called by the runtime. Use this method to add services to the container. 23 | public void ConfigureServices(IServiceCollection services) 24 | { 25 | services.AddDbContext(opts => opts.UseSqlServer(Configuration["ConnectionString:EmployeeDB"])); 26 | services.AddScoped, EmployeeManager>(); 27 | services.AddControllers(); 28 | } 29 | 30 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 31 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 32 | { 33 | if (env.IsDevelopment()) 34 | { 35 | app.UseDeveloperExceptionPage(); 36 | } 37 | 38 | app.UseHttpsRedirection(); 39 | 40 | app.UseRouting(); 41 | 42 | app.UseAuthorization(); 43 | 44 | app.UseEndpoints(endpoints => 45 | { 46 | endpoints.MapControllers(); 47 | }); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081010_EFCoreCodeFirstSample.Models.EmployeeContext.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using EFCoreCodeFirstSample.Models; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace EFCoreCodeFirstSample.Migrations 11 | { 12 | [DbContext(typeof(EmployeeContext))] 13 | [Migration("20191024081010_EFCoreCodeFirstSample.Models.EmployeeContext")] 14 | partial class EFCoreCodeFirstSampleModelsEmployeeContext 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("EFCoreCodeFirstSample.Models.Employee", b => 25 | { 26 | b.Property("EmployeeId") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("bigint") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("DateOfBirth") 32 | .HasColumnType("datetime2"); 33 | 34 | b.Property("Email") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("FirstName") 38 | .HasColumnType("nvarchar(max)"); 39 | 40 | b.Property("LastName") 41 | .HasColumnType("nvarchar(max)"); 42 | 43 | b.Property("PhoneNumber") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("EmployeeId"); 47 | 48 | b.ToTable("Employees"); 49 | }); 50 | #pragma warning restore 612, 618 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Controllers/EmployeeController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using EFCoreCodeFirstSample.Models; 3 | using EFCoreCodeFirstSample.Models.Repository; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace EFCoreCodeFirstSample.Controllers 7 | { 8 | [Route("api/employee")] 9 | [ApiController] 10 | public class EmployeeController : ControllerBase 11 | { 12 | private readonly IDataRepository _dataRepository; 13 | 14 | public EmployeeController(IDataRepository dataRepository) 15 | { 16 | _dataRepository = dataRepository; 17 | } 18 | 19 | // GET: api/Employee 20 | [HttpGet] 21 | public IActionResult Get() 22 | { 23 | IEnumerable employees = _dataRepository.GetAll(); 24 | return Ok(employees); 25 | } 26 | 27 | // GET: api/Employee/5 28 | [HttpGet("{id}", Name = "Get")] 29 | public IActionResult Get(long id) 30 | { 31 | Employee employee = _dataRepository.Get(id); 32 | 33 | if (employee == null) 34 | { 35 | return NotFound("The Employee record couldn't be found."); 36 | } 37 | 38 | return Ok(employee); 39 | } 40 | 41 | // POST: api/Employee 42 | [HttpPost] 43 | public IActionResult Post([FromBody] Employee employee) 44 | { 45 | if (employee == null) 46 | { 47 | return BadRequest("Employee is null."); 48 | } 49 | 50 | _dataRepository.Add(employee); 51 | return CreatedAtRoute( 52 | "Get", 53 | new { Id = employee.EmployeeId }, 54 | employee); 55 | } 56 | 57 | // PUT: api/Employee/5 58 | [HttpPut("{id}")] 59 | public IActionResult Put(long id, [FromBody] Employee employee) 60 | { 61 | if (employee == null) 62 | { 63 | return BadRequest("Employee is null."); 64 | } 65 | 66 | Employee employeeToUpdate = _dataRepository.Get(id); 67 | if (employeeToUpdate == null) 68 | { 69 | return NotFound("The Employee record couldn't be found."); 70 | } 71 | 72 | _dataRepository.Update(employeeToUpdate, employee); 73 | return NoContent(); 74 | } 75 | 76 | // DELETE: api/Employee/5 77 | [HttpDelete("{id}")] 78 | public IActionResult Delete(long id) 79 | { 80 | Employee employee = _dataRepository.Get(id); 81 | if (employee == null) 82 | { 83 | return NotFound("The Employee record couldn't be found."); 84 | } 85 | 86 | _dataRepository.Delete(employee); 87 | return NoContent(); 88 | } 89 | } 90 | } -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/EmployeeContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using EFCoreCodeFirstSample.Models; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace EFCoreCodeFirstSample.Migrations 10 | { 11 | [DbContext(typeof(EmployeeContext))] 12 | partial class EmployeeContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "3.0.0") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("EFCoreCodeFirstSample.Models.Employee", b => 23 | { 24 | b.Property("EmployeeId") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnType("bigint") 27 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 28 | 29 | b.Property("DateOfBirth") 30 | .HasColumnType("datetime2"); 31 | 32 | b.Property("Email") 33 | .HasColumnType("nvarchar(max)"); 34 | 35 | b.Property("FirstName") 36 | .HasColumnType("nvarchar(max)"); 37 | 38 | b.Property("Gender") 39 | .HasColumnType("nvarchar(max)"); 40 | 41 | b.Property("LastName") 42 | .HasColumnType("nvarchar(max)"); 43 | 44 | b.Property("PhoneNumber") 45 | .HasColumnType("nvarchar(max)"); 46 | 47 | b.HasKey("EmployeeId"); 48 | 49 | b.ToTable("Employees"); 50 | 51 | b.HasData( 52 | new 53 | { 54 | EmployeeId = 1L, 55 | DateOfBirth = new DateTime(1979, 4, 25, 0, 0, 0, 0, DateTimeKind.Unspecified), 56 | Email = "uncle.bob@gmail.com", 57 | FirstName = "Uncle", 58 | LastName = "Bob", 59 | PhoneNumber = "999-888-7777" 60 | }, 61 | new 62 | { 63 | EmployeeId = 2L, 64 | DateOfBirth = new DateTime(1981, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), 65 | Email = "jan.kirsten@gmail.com", 66 | FirstName = "Jan", 67 | LastName = "Kirsten", 68 | PhoneNumber = "111-222-3333" 69 | }); 70 | }); 71 | #pragma warning restore 612, 618 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081149_EFCoreCodeFirstSample.Models.EmployeeContextSeed.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using EFCoreCodeFirstSample.Models; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace EFCoreCodeFirstSample.Migrations 11 | { 12 | [DbContext(typeof(EmployeeContext))] 13 | [Migration("20191024081149_EFCoreCodeFirstSample.Models.EmployeeContextSeed")] 14 | partial class EFCoreCodeFirstSampleModelsEmployeeContextSeed 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("EFCoreCodeFirstSample.Models.Employee", b => 25 | { 26 | b.Property("EmployeeId") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("bigint") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("DateOfBirth") 32 | .HasColumnType("datetime2"); 33 | 34 | b.Property("Email") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("FirstName") 38 | .HasColumnType("nvarchar(max)"); 39 | 40 | b.Property("LastName") 41 | .HasColumnType("nvarchar(max)"); 42 | 43 | b.Property("PhoneNumber") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("EmployeeId"); 47 | 48 | b.ToTable("Employees"); 49 | 50 | b.HasData( 51 | new 52 | { 53 | EmployeeId = 1L, 54 | DateOfBirth = new DateTime(1979, 4, 25, 0, 0, 0, 0, DateTimeKind.Unspecified), 55 | Email = "uncle.bob@gmail.com", 56 | FirstName = "Uncle", 57 | LastName = "Bob", 58 | PhoneNumber = "999-888-7777" 59 | }, 60 | new 61 | { 62 | EmployeeId = 2L, 63 | DateOfBirth = new DateTime(1981, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), 64 | Email = "jan.kirsten@gmail.com", 65 | FirstName = "Jan", 66 | LastName = "Kirsten", 67 | PhoneNumber = "111-222-3333" 68 | }); 69 | }); 70 | #pragma warning restore 612, 618 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /EFCoreCodeFirstSample/EFCoreCodeFirstSample/Migrations/20191024081309_EFCoreCodeFirstSample.Models.AddEmployeeGender.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using EFCoreCodeFirstSample.Models; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace EFCoreCodeFirstSample.Migrations 11 | { 12 | [DbContext(typeof(EmployeeContext))] 13 | [Migration("20191024081309_EFCoreCodeFirstSample.Models.AddEmployeeGender")] 14 | partial class EFCoreCodeFirstSampleModelsAddEmployeeGender 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("EFCoreCodeFirstSample.Models.Employee", b => 25 | { 26 | b.Property("EmployeeId") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("bigint") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("DateOfBirth") 32 | .HasColumnType("datetime2"); 33 | 34 | b.Property("Email") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("FirstName") 38 | .HasColumnType("nvarchar(max)"); 39 | 40 | b.Property("Gender") 41 | .HasColumnType("nvarchar(max)"); 42 | 43 | b.Property("LastName") 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.Property("PhoneNumber") 47 | .HasColumnType("nvarchar(max)"); 48 | 49 | b.HasKey("EmployeeId"); 50 | 51 | b.ToTable("Employees"); 52 | 53 | b.HasData( 54 | new 55 | { 56 | EmployeeId = 1L, 57 | DateOfBirth = new DateTime(1979, 4, 25, 0, 0, 0, 0, DateTimeKind.Unspecified), 58 | Email = "uncle.bob@gmail.com", 59 | FirstName = "Uncle", 60 | LastName = "Bob", 61 | PhoneNumber = "999-888-7777" 62 | }, 63 | new 64 | { 65 | EmployeeId = 2L, 66 | DateOfBirth = new DateTime(1981, 7, 13, 0, 0, 0, 0, DateTimeKind.Unspecified), 67 | Email = "jan.kirsten@gmail.com", 68 | FirstName = "Jan", 69 | LastName = "Kirsten", 70 | PhoneNumber = "111-222-3333" 71 | }); 72 | }); 73 | #pragma warning restore 612, 618 74 | } 75 | } 76 | } 77 | --------------------------------------------------------------------------------