Stevepy

TPH Example

Sep 4th, 2024 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | Source Code | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4.  
  5. namespace EFCore6Tests.TPH
  6. {
  7.     [Table("Client", Schema = "TPH")]
  8.     public class Client
  9.     {
  10.         [Key]
  11.         public int ClientId { get; protected set; }
  12.         public string Name { get; set; }
  13.         public virtual ClientAddress ClientAddress { get; set; }
  14.  
  15. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  16.         public Client() { }
  17. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  18.     }
  19.  
  20.     [Table("Address", Schema = "TPH")]
  21.     public abstract class Address : IAddress
  22.     {
  23.         [Key]
  24.         public int AddressId { get; protected set; }
  25.         public string Name { get; set; }
  26.         public string? UnitNum { get; set; }
  27.         public string StreetNum { get; set; }
  28.         public string StreetName { get; set; }
  29.         public string StreetType { get; set; }
  30.         public int SuburbId { get; set; }
  31.         public string City { get; set; }
  32.  
  33.         int IAddress.Id { get => AddressId; }
  34.  
  35.         public abstract string AddressType { get; }
  36.  
  37. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  38.         protected Address() { }
  39. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  40.  
  41.         public Address(string name, string? unitNum, string streetNum, string streetName, string streetType, int suburbId, string city)
  42.         {
  43.             Name = name;
  44.             UnitNum = unitNum;
  45.             StreetNum = streetNum;
  46.             StreetName = streetName;
  47.             StreetType = streetType;
  48.             SuburbId = suburbId;
  49.             City = city;
  50.         }
  51.     }
  52.  
  53.     public class ClientAddress : Address
  54.     {
  55.         public override string AddressType => typeof(ClientAddress).Name;
  56.  
  57.         public virtual Client? Client { get; set; }
  58.  
  59. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  60.         public ClientAddress() { }
  61. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  62.  
  63.         public ClientAddress(string name, string? unitNum, string streetNum, string streetName, string streetType, int suburbId, string city)
  64.             : base(name, unitNum, streetNum, streetName, streetType, suburbId, city) { }
  65.  
  66.     }
  67.  
  68.     public interface IAddress
  69.     {
  70.         public int Id { get; }
  71.         string Name { get; }
  72.         string? UnitNum { get; }
  73.         string StreetNum { get; }
  74.         string StreetName { get; }
  75.         string StreetType { get; }
  76.         int SuburbId { get; }
  77.         string City { get; }
  78.     }
  79.  
  80.     public class TPHDbContext : DbContext
  81.     {
  82.         public DbSet<Client> Clients { get; set; }
  83.         public DbSet<ClientAddress> ClientAddresses { get; set; }
  84.  
  85.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  86.         {
  87.             optionsBuilder.UseSqlServer(@"Data Source=Machine\SQLEXPRESS;Initial Catalog=Inheritance;Trusted_Connection=Yes;MultipleActiveResultSets=True;TrustServerCertificate=True");
  88.             base.OnConfiguring(optionsBuilder);
  89.         }
  90.  
  91.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  92.         {
  93.             base.OnModelCreating(modelBuilder);
  94.  
  95.             modelBuilder.Entity<Address>()
  96.                 .ToTable("Address", "TPH")
  97.                 .HasDiscriminator<string>("Discriminator")
  98.                 .HasValue<ClientAddress>(typeof(ClientAddress).Name);
  99.  
  100.             modelBuilder.Entity<Client>()
  101.                 .HasOne(x => x.ClientAddress)
  102.                 .WithOne(x => x.Client)
  103.                 .HasForeignKey<Client>("AddressId");
  104.         }
  105.     }
  106. }
  107.  
  108. using EFCore6Tests.TPH;
  109. using Microsoft.EntityFrameworkCore;
  110. using NUnit.Framework;
  111.  
  112. namespace EFCore6Tests
  113. {
  114.     [TestFixture]
  115.     public class TPHTests
  116.     {
  117.         [Test]
  118.         public void CreateTest()
  119.         {
  120.             using var context = new TPHDbContext();
  121.  
  122.             var client = new Client
  123.             {
  124.                 Name = "Test1",
  125.                 ClientAddress = new ClientAddress { Name = "Test", StreetNum = "1a", StreetName = "Pelican", SuburbId = 2, StreetType = "Drive", City = "Bay City" }
  126.             };
  127.  
  128.             context.Clients.Add(client);
  129.             context.SaveChanges();
  130.         }
  131.  
  132.         [Test]
  133.         public void ReadTest()
  134.         {
  135.             using var context = new TPHDbContext();
  136.             var clients = context.Clients.Include(x => x.ClientAddress).ToList();
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment