├── EFExamples.Sql
├── FileGroups
│ ├── EmployeeDocuments.sql
│ └── EmployeeProfileImages.sql
├── Tables
│ ├── FileType.sql
│ ├── DocumentType.sql
│ ├── __EFMigrationsHistory.sql
│ ├── Vendor.sql
│ ├── Company.sql
│ ├── EmployeeDocument.sql
│ ├── FileDescription.sql
│ ├── Department.sql
│ ├── DepartmentContractor.sql
│ ├── Contractor.sql
│ └── Employee.sql
└── EFExamples.Sql.sqlproj
├── EFExamples.Api
├── appsettings.json
├── appsettings.Development.json
├── EFExamples.Api.csproj
├── Program.cs
├── Controllers
│ └── ValuesController.cs
└── Startup.cs
├── EFExamples.Models
├── Entities
│ ├── DocumentType.cs
│ ├── Vendor.cs
│ ├── EmployeeDocument.cs
│ ├── Entity.cs
│ ├── Company.cs
│ ├── FileDescription.cs
│ ├── DepartmentContractor.cs
│ ├── Department.cs
│ ├── Contractor.cs
│ └── Employee.cs
├── EFExamples.Models.csproj
└── ValueTypes
│ ├── PersonName.cs
│ ├── Address.cs
│ └── ValueObject.cs
├── README.md
├── EFExamples.Data
├── Configuration
│ ├── DocumentTypeConfiguration.cs
│ ├── VendorConfiguration.cs
│ ├── EmployeeDocumentConfiguration.cs
│ ├── EntityConfiguration.cs
│ ├── DepartmentContractorConfiguration.cs
│ ├── FileDescriptionConfiguration.cs
│ ├── CompanyConfiguration.cs
│ ├── DepartmentConfiguration.cs
│ ├── ContractorConfiguration.cs
│ └── EmployeeConfiguration.cs
├── DataConstants.cs
├── Queries
│ └── EmployeeQuery.cs
├── Projections
│ └── EmployeeProjection.cs
├── Commands
│ └── EmployeeCommand.cs
├── EFExamples.Data.csproj
├── AssemblyInfo.cs
├── EFExamplesDbContext.cs
└── Migrations
│ ├── 20190127160330_InitialCreate.cs
│ ├── EFExamplesDbContextModelSnapshot.cs
│ └── 20190127160330_InitialCreate.Designer.cs
├── EFExamples.Tests
├── EFExamples.Tests.csproj
├── EmployeeTests.cs
├── ModelFakes.cs
├── ValueObjectTests.cs
├── InMemoryDbTests.cs
└── DataGenerator.cs
├── EFExamples.sln
└── .gitignore
/EFExamples.Sql/FileGroups/EmployeeDocuments.sql:
--------------------------------------------------------------------------------
1 | ALTER DATABASE [$(DatabaseName)]
2 | ADD FILEGROUP [EmployeeDocuments] CONTAINS FILESTREAM
3 |
--------------------------------------------------------------------------------
/EFExamples.Sql/FileGroups/EmployeeProfileImages.sql:
--------------------------------------------------------------------------------
1 |
2 | ALTER DATABASE [$(DatabaseName)]
3 | ADD FILEGROUP [EmployeeProfileImages] CONTAINS FILESTREAM
4 |
--------------------------------------------------------------------------------
/EFExamples.Api/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/DocumentType.cs:
--------------------------------------------------------------------------------
1 | namespace EFExamples.Models.Entities
2 | {
3 | public class DocumentType : Entity
4 | {
5 | public string Name { get; set; }
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/EFExamples.Models/EFExamples.Models.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.1
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/EFExamples.Api/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/FileType.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[FileType]
2 | (
3 | [Id] INT NOT NULL PRIMARY KEY,
4 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
5 | [DateUpdated] DATETIME2 (2) NULL,
6 | [Name] VARCHAR(300) NOT NULL
7 | )
8 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/DocumentType.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[DocumentType]
2 | (
3 | [Id] INT NOT NULL IDENTITY PRIMARY KEY,
4 | [Name] VARCHAR(300) NOT NULL DEFAULT (''),
5 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
6 | [DateUpdated] DATETIME2 (2) NULL,
7 | )
8 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/__EFMigrationsHistory.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[__EFMigrationsHistory] (
2 | [MigrationId] NVARCHAR (150) NOT NULL,
3 | [ProductVersion] NVARCHAR (32) NOT NULL,
4 | CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY CLUSTERED ([MigrationId] ASC)
5 | );
6 |
7 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Vendor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public class Vendor : Entity
8 | {
9 | public string Name { get; set; }
10 |
11 | public ICollection Contractors { get; set; }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/Vendor.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[Vendor] (
2 | [Id] INT IDENTITY (1, 1) NOT NULL,
3 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
4 | [DateUpdated] DATETIME2 (2) NULL,
5 | [Name] NVARCHAR (300) DEFAULT (N'') NULL,
6 | CONSTRAINT [PK_Vendor] PRIMARY KEY CLUSTERED ([Id] ASC)
7 | );
8 |
9 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/Company.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[Company] (
2 | [Id] INT IDENTITY (1, 1) NOT NULL,
3 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
4 | [DateUpdated] DATETIME2 (2) NULL,
5 | [Name] NVARCHAR (300) DEFAULT (N'') NULL,
6 | CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED ([Id] ASC)
7 | );
8 |
9 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/EmployeeDocument.cs:
--------------------------------------------------------------------------------
1 | namespace EFExamples.Models.Entities
2 | {
3 | public class EmployeeDocument
4 | {
5 | public int FileDescriptionId { get; set; }
6 | public int EmployeeId { get; set; }
7 |
8 | public Employee Employee { get; set; }
9 | public FileDescription FileDescription { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Entity.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public abstract class Entity
8 | {
9 | public int Id { get; set; }
10 | public DateTime DateCreated { get; set; }
11 | public DateTime? DateUpdated { get; set; }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Company.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public class Company : Entity
8 | {
9 | public string Name { get; set; }
10 |
11 | public ICollection Employees { get; set; }
12 | public ICollection Departments { get; set; }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/FileDescription.cs:
--------------------------------------------------------------------------------
1 | namespace EFExamples.Models.Entities
2 | {
3 | public class FileDescription : Entity
4 | {
5 | public int DocumentTypeId { get; set; }
6 | public string ContentType { get; set; }
7 | public string Description { get; set; }
8 | public string FileName { get; set; }
9 |
10 | public DocumentType DocumentType { get; set; }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/DepartmentContractor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public class DepartmentContractor
8 | {
9 | public int ContractorId { get; set; }
10 | public int DepartmentId { get; set; }
11 |
12 | public Contractor Contractor { get; set; }
13 | public Department Department { get; set; }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/EmployeeDocument.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[EmployeeDocument]
2 | (
3 | [EmployeeId] INT NOT NULL,
4 | [FileDescriptionId] INT NOT NULL,
5 | CONSTRAINT [FK_EmployeeDocument_Employee_EmployeeId] FOREIGN KEY ([EmployeeId]) REFERENCES [Employee]([Id]),
6 | CONSTRAINT [FK_EmployeeDocument_FileDescription_FileDescriptionId] FOREIGN KEY ([FileDescriptionId]) REFERENCES [FileDescription]([Id]),
7 | CONSTRAINT [PK_EmployeeDocument] PRIMARY KEY ([EmployeeId], [FileDescriptionId])
8 | )
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Entity Framework Core Examples
2 | Examples for working with Entity Framework Core
3 | **Value Objects**: https://edgesidesolutions.com/ddd-value-objects-with-entity-framework-core/
4 | **Sql Server Guid Index**: https://edgesidesolutions.com/index-sql-server-guid-column/
5 | **CQRS with Entity Framework Core**: https://edgesidesolutions.com/cqrs-with-entity-framework-core/
6 | **Testing with Entity Framework Core**: https://edgesidesolutions.com/testing-with-entity-framework-core/
7 |
--------------------------------------------------------------------------------
/EFExamples.Api/EFExamples.Api.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.1
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/DocumentTypeConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
3 |
4 | namespace EFExamples.Data.Configuration
5 | {
6 | public class DocumentTypeConfiguration : EntityConfiguration
7 | {
8 | public override void Configure(EntityTypeBuilder builder)
9 | {
10 | builder.Property(p => p.Name).HasMaxLength(300);
11 |
12 | base.Configure(builder);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/EFExamples.Data/DataConstants.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Data
6 | {
7 | public class DataConstants
8 | {
9 | public class SqlServer
10 | {
11 | public const string NewId = "newid()";
12 | public const string SysDateTime = "sysdatetime()";
13 | public const string NewSequentialId = "newsequentialid()";
14 | public const string DateTime2 = "datetime2(2)";
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Department.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public class Department : Entity
8 | {
9 | public int CompanyId { get; set; }
10 |
11 | public string Name { get; set; }
12 |
13 | public Company Company { get; set; }
14 | public ICollection DepartmentContractors { get; set; }
15 | public ICollection Employees { get; set; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/FileDescription.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[FileDescription]
2 | (
3 | [Id] INT NOT NULL IDENTITY PRIMARY KEY,
4 | [DocumentTypeId] INT NOT NULL,
5 | [ContentType] VARCHAR(150) NOT NULL DEFAULT(''),
6 | [Description] VARCHAR(400) NOT NULL DEFAULT(''),
7 | [FileName] VARCHAR(400) NOT NULL DEFAULT(''),
8 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
9 | [DateUpdated] DATETIME2 (2) NULL
10 | CONSTRAINT [FK_FileDescription_DocumentType_DocumentTypeId] FOREIGN KEY ([DocumentTypeId]) REFERENCES [DocumentType]([Id])
11 | )
12 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Contractor.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.ValueTypes;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace EFExamples.Models.Entities
7 | {
8 | public class Contractor : Entity
9 | {
10 | public int VendorId { get; set; }
11 |
12 | public Address Address { get; set; }
13 | public DateTime DateOfBirth { get; set; }
14 | public PersonName Name { get; set; }
15 |
16 | public ICollection DepartmentContractors { get; set; }
17 | public Vendor Vendor { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/EFExamples.Data/Queries/EmployeeQuery.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore;
3 | using System;
4 | using System.Linq;
5 | using System.Linq.Expressions;
6 | using System.Threading.Tasks;
7 |
8 | namespace EFExamples.Data.Queries
9 | {
10 | public static class EmployeeQuery
11 | {
12 | public static async Task GetEmployeeByIdAsync(this DbSet employeeDbSet, Expression> projection, int employeeId)
13 | {
14 | return await employeeDbSet.Where(m => m.Id == employeeId).Select(projection).FirstOrDefaultAsync();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/Department.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[Department] (
2 | [Id] INT IDENTITY (1, 1) NOT NULL,
3 | [DateCreated] DATETIME2 (2) DEFAULT (sysdatetime()) NOT NULL,
4 | [DateUpdated] DATETIME2 (2) NULL,
5 | [CompanyId] INT NOT NULL,
6 | [Name] NVARCHAR (300) DEFAULT (N'') NULL,
7 | CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([Id] ASC),
8 | CONSTRAINT [FK_Department_Company_CompanyId] FOREIGN KEY ([CompanyId]) REFERENCES [dbo].[Company] ([Id]) ON DELETE CASCADE
9 | );
10 |
11 |
12 | GO
13 | CREATE NONCLUSTERED INDEX [IX_Department_CompanyId]
14 | ON [dbo].[Department]([CompanyId] ASC);
15 |
16 |
--------------------------------------------------------------------------------
/EFExamples.Api/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore;
7 | using Microsoft.AspNetCore.Hosting;
8 | using Microsoft.Extensions.Configuration;
9 | using Microsoft.Extensions.Logging;
10 |
11 | namespace EFExamples.Api
12 | {
13 | public class Program
14 | {
15 | public static void Main(string[] args)
16 | {
17 | CreateWebHostBuilder(args).Build().Run();
18 | }
19 |
20 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
21 | WebHost.CreateDefaultBuilder(args)
22 | .UseStartup();
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/EFExamples.Sql/Tables/DepartmentContractor.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE [dbo].[DepartmentContractor] (
2 | [ContractorId] INT NOT NULL,
3 | [DepartmentId] INT NOT NULL,
4 | CONSTRAINT [PK_DepartmentContractor] PRIMARY KEY CLUSTERED ([ContractorId] ASC, [DepartmentId] ASC),
5 | CONSTRAINT [FK_DepartmentContractor_Contractor_ContractorId] FOREIGN KEY ([ContractorId]) REFERENCES [dbo].[Contractor] ([Id]) ON DELETE CASCADE,
6 | CONSTRAINT [FK_DepartmentContractor_Department_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([Id]) ON DELETE CASCADE
7 | );
8 |
9 |
10 | GO
11 | CREATE NONCLUSTERED INDEX [IX_DepartmentContractor_DepartmentId]
12 | ON [dbo].[DepartmentContractor]([DepartmentId] ASC);
13 |
14 |
--------------------------------------------------------------------------------
/EFExamples.Models/Entities/Employee.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.ValueTypes;
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | namespace EFExamples.Models.Entities
6 | {
7 | public class Employee : Entity
8 | {
9 | public int CompanyId { get; set; }
10 | public int DepartmentId { get; set; }
11 | public Guid EmployeeId { get; private set; }
12 |
13 | public Address Address { get; set; }
14 | public DateTime DateOfBirth { get; set; }
15 | public PersonName Name { get; set; }
16 |
17 | public Company Company { get; set; }
18 | public Department Department { get; set; }
19 | public ICollection EmployeeDocuments { get; set; }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/VendorConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore;
3 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Text;
7 |
8 | namespace EFExamples.Data.Configuration
9 | {
10 | public class VendorConfiguration : EntityConfiguration
11 | {
12 | public override void Configure(EntityTypeBuilder builder)
13 | {
14 | builder.Property(p => p.Name).HasMaxLength(300).HasDefaultValue("");
15 |
16 | builder.HasMany(m => m.Contractors).WithOne(m => m.Vendor).HasForeignKey(k => k.VendorId);
17 |
18 | base.Configure(builder);
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/EmployeeDocumentConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore;
3 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
4 |
5 | namespace EFExamples.Data.Configuration
6 | {
7 | public class EmployeeDocumentConfiguration : IEntityTypeConfiguration
8 | {
9 | public void Configure(EntityTypeBuilder builder)
10 | {
11 | builder.HasKey(m => new { m.EmployeeId, m.FileDescriptionId });
12 | builder.HasOne(m => m.Employee).WithMany(m => m.EmployeeDocuments).HasForeignKey(k => k.EmployeeId);
13 | builder.HasOne(m => m.FileDescription).WithMany().HasForeignKey(k => k.FileDescriptionId);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/EFExamples.Data/Projections/EmployeeProjection.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using System;
3 | using System.Linq.Expressions;
4 |
5 | namespace EFExamples.Data.Projections
6 | {
7 | public static class EmployeeProjection
8 | {
9 | public static Expression> EmployeeWithoutDocuments
10 | {
11 | get
12 | {
13 | return m => new
14 | {
15 | m.Name,
16 | m.Company,
17 | m.Address,
18 | m.EmployeeId,
19 | m.Id,
20 | m.DepartmentId,
21 | m.Department,
22 | m.CompanyId
23 | };
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/EntityConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models;
2 | using EFExamples.Models.Entities;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Text;
8 |
9 | namespace EFExamples.Data.Configuration
10 | {
11 | public abstract class EntityConfiguration : IEntityTypeConfiguration
12 | where T : Entity
13 | {
14 | public virtual void Configure(EntityTypeBuilder builder)
15 | {
16 | builder.Property(m => m.DateCreated).HasColumnType(DataConstants.SqlServer.DateTime2).HasDefaultValueSql(DataConstants.SqlServer.SysDateTime);
17 | builder.Property(p => p.DateUpdated).HasColumnType(DataConstants.SqlServer.DateTime2);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/EFExamples.Tests/EFExamples.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.1
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/EFExamples.Data/Commands/EmployeeCommand.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using EFExamples.Models.ValueTypes;
3 | using Microsoft.EntityFrameworkCore;
4 | using System.Threading.Tasks;
5 |
6 | namespace EFExamples.Data.Commands
7 | {
8 | public static class EmployeeCommand
9 | {
10 | public static async Task UpdateEmployee(this DbSet employeeDbSet, Employee employee)
11 | {
12 | var existingEmployee = await employeeDbSet.FirstOrDefaultAsync(m => m.Id == employee.Id);
13 | existingEmployee.Name = new PersonName(employee.Name.FirstName, employee.Name.LastName);
14 | existingEmployee.Address = new Address(employee.Address.StreetAddress, employee.Address.City, employee.Address.State, employee.Address.ZipCode);
15 | existingEmployee.DateOfBirth = employee.DateOfBirth;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/DepartmentContractorConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore;
3 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Text;
7 |
8 | namespace EFExamples.Data.Configuration
9 | {
10 | public class DepartmentContractorConfiguration : IEntityTypeConfiguration
11 | {
12 | public void Configure(EntityTypeBuilder builder)
13 | {
14 | builder.HasKey(m => new { m.ContractorId, m.DepartmentId });
15 | builder.HasOne(m => m.Contractor).WithMany(m => m.DepartmentContractors).HasForeignKey(k => k.ContractorId);
16 | builder.HasOne(m => m.Department).WithMany(m => m.DepartmentContractors).HasForeignKey(k => k.DepartmentId);
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/FileDescriptionConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models.Entities;
2 | using Microsoft.EntityFrameworkCore;
3 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
4 |
5 | namespace EFExamples.Data.Configuration
6 | {
7 | public class FileDescriptionConfiguration : EntityConfiguration
8 | {
9 | public override void Configure(EntityTypeBuilder builder)
10 | {
11 | builder.Property(p => p.ContentType).HasDefaultValue("").HasMaxLength(300);
12 | builder.Property(p => p.Description).HasDefaultValue("").HasMaxLength(1500);
13 | builder.Property(p => p.FileName).HasDefaultValue("").HasMaxLength(250);
14 | builder.HasOne(m => m.DocumentType).WithMany().HasForeignKey(k => k.DocumentTypeId);
15 |
16 | base.Configure(builder);
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/EFExamples.Data/Configuration/CompanyConfiguration.cs:
--------------------------------------------------------------------------------
1 | using EFExamples.Models;
2 | using EFExamples.Models.Entities;
3 | using Microsoft.EntityFrameworkCore;
4 | using Microsoft.EntityFrameworkCore.Metadata.Builders;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Text;
8 |
9 | namespace EFExamples.Data.Configuration
10 | {
11 | public class CompanyConfiguration : EntityConfiguration
12 | {
13 | public override void Configure(EntityTypeBuilder builder)
14 | {
15 | builder.Property(p => p.Name).HasMaxLength(300).HasDefaultValue("");
16 |
17 | builder.HasMany(m => m.Employees).WithOne(m => m.Company).HasForeignKey(k => k.CompanyId);
18 | builder.HasMany(m => m.Departments).WithOne(m => m.Company).HasForeignKey(k => k.CompanyId);
19 |
20 | base.Configure(builder);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/EFExamples.Models/ValueTypes/PersonName.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace EFExamples.Models.ValueTypes
6 | {
7 | public class PersonName : ValueObject
8 | {
9 | public string FirstName { get; private set; }
10 | public string FullName
11 | {
12 | get
13 | {
14 | return $"{FirstName} {LastName}";
15 | }
16 | }
17 | public string LastName { get; private set; }
18 |
19 | private PersonName()
20 | {
21 |
22 | }
23 |
24 | public PersonName(string firstName, string lastName)
25 | {
26 | FirstName = firstName;
27 | LastName = lastName;
28 | }
29 |
30 | protected override IEnumerable