Stevepy

TPT Example

Sep 4th, 2024 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | Source Code | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4.  
  5. namespace EFCore6Tests.TPT
  6. {
  7.  
  8.     [Table("Client", Schema = "TPT")]
  9.     public class Client
  10.     {
  11.         [Key]
  12.         public int ClientId { get; protected set; }
  13.         public string Name { get; set; }
  14.         public virtual ClientAddress ClientAddress { get; set; }
  15.  
  16. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  17.         public Client() { }
  18. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  19.     }
  20.  
  21.     [Table("Supplier", Schema = "TPT")]
  22.     public class Supplier
  23.     {
  24.         [Key]
  25.         public int SupplierId { get; protected set; }
  26.         public string Name { get; set; }
  27.         public virtual SupplierAddress SupplierAddress { get; set; }
  28.  
  29. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  30.         public Supplier() { }
  31. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  32.     }
  33.  
  34.     [Table("Address", Schema = "TPT")]
  35.     public abstract class Address
  36.     {
  37.         [Key]
  38.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  39.         public int AddressId { get; protected set; }
  40.         public string Name { get; set; }
  41.         public string? UnitNum { get; set; }
  42.         public string StreetNum { get; set; }
  43.         public string StreetName { get; set; }
  44.         public string StreetType { get; set; }
  45.         public int SuburbId { get; set; }
  46.         public string City { get; set; }
  47.  
  48. #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  49.         public Address() { }
  50. #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
  51.  
  52.         public Address(string name, string? unitNum, string streetNum, string streetName, string streetType, int suburbId, string city)
  53.         {
  54.             Name = name;
  55.             UnitNum = unitNum;
  56.             StreetNum = streetNum;
  57.             StreetName = streetName;
  58.             StreetType = streetType;
  59.             SuburbId = suburbId;
  60.             City = city;
  61.         }
  62.     }
  63.  
  64.     [Table("ClientAddress", Schema = "TPT")]
  65.     public class ClientAddress : Address, IAddress
  66.     {
  67.         int IAddress.Id { get => AddressId; }
  68.  
  69.         string IAddress.AddressType
  70.         {
  71.             get => typeof(ClientAddress).Name;
  72.         }
  73.  
  74.         public virtual Client? Client { get; set; }
  75.     }
  76.  
  77.     [Table("SupplierAddress", Schema = "TPT")]
  78.     public class SupplierAddress : Address, IAddress
  79.     {
  80.         int IAddress.Id { get => AddressId; }
  81.  
  82.         string IAddress.AddressType
  83.         {
  84.             get => typeof(SupplierAddress).Name;
  85.         }
  86.  
  87.         public virtual Supplier? Supplier { get; set; }
  88.     }
  89.  
  90.     public interface IAddress
  91.     {
  92.         public int Id { get; }
  93.         string AddressType { get; }
  94.  
  95.         string Name { get; }
  96.         string? UnitNum { get; }
  97.         string StreetNum { get; }
  98.         string StreetName { get; }
  99.         string StreetType { get; }
  100.         int SuburbId { get; }
  101.         string City { get; }
  102.     }
  103.  
  104.     public class TPTDbContext : DbContext
  105.     {
  106.         public DbSet<Client> Clients { get; set; }
  107.         public DbSet<Supplier> Suppliers { get; set; }
  108.  
  109.         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  110.         {
  111.             optionsBuilder.UseSqlServer(@"Data Source=Machine\SQLEXPRESS;Initial Catalog=Inheritance;Trusted_Connection=Yes;MultipleActiveResultSets=True;TrustServerCertificate=True");
  112.             base.OnConfiguring(optionsBuilder);
  113.         }
  114.  
  115.         protected override void OnModelCreating(ModelBuilder modelBuilder)
  116.         {
  117.             base.OnModelCreating(modelBuilder);
  118.  
  119.             modelBuilder.Entity<Address>()
  120.                 .ToTable("Address", "TPT");
  121.  
  122.             modelBuilder.Entity<Client>()
  123.                 .HasOne(x => x.ClientAddress)
  124.                 .WithOne(x => x.Client)
  125.                 .HasForeignKey<Client>("ClientAddressId");
  126.  
  127.             modelBuilder.Entity<Supplier>()
  128.                 .HasOne(x => x.SupplierAddress)
  129.                 .WithOne(x => x.Supplier)
  130.                 .HasForeignKey<Supplier>("SupplierAddressId");
  131.         }
  132.     }
  133. }
  134.  
  135. using EFCore6Tests.TPT;
  136. using Microsoft.EntityFrameworkCore;
  137. using NUnit.Framework;
  138.  
  139. namespace EFCore6Tests
  140. {
  141.     [TestFixture]
  142.     public class TPTTests
  143.     {
  144.         [Test]
  145.         public void CreateTest()
  146.         {
  147.             using var context = new TPTDbContext();
  148.  
  149.             var client = new Client
  150.             {
  151.                 Name = "Test1",
  152.                 ClientAddress = new ClientAddress { Name = "Test", StreetNum = "1a", StreetName = "Pelican", SuburbId = 2, StreetType = "Drive", City = "Bay City" }
  153.             };
  154.  
  155.             context.Clients.Add(client);
  156.             context.SaveChanges();
  157.         }
  158.  
  159.         [Test]
  160.         public void ReadTest()
  161.         {
  162.             using var context = new TPTDbContext();
  163.             var clients = context.Clients.Include(x => x.ClientAddress).ToList();
  164.         }
  165.  
  166.         [Test]
  167.         public void AssociateAddressToSupplier()
  168.         {
  169.             using var context = new TPTDbContext();
  170.             var client = context.Clients.Include(x => x.ClientAddress).First();
  171.  
  172.             var supplier = new Supplier
  173.             {
  174.                 Name = "Test",
  175.                 SupplierAddress = new SupplierAddress
  176.                 {
  177.                     Name = client.ClientAddress.Name,
  178.                     StreetName = client.ClientAddress.StreetName,
  179.                     StreetNum = client.ClientAddress.StreetNum,
  180.                     StreetType = client.ClientAddress.StreetType,
  181.                     SuburbId = client.ClientAddress.SuburbId,
  182.                     City = client.ClientAddress.City,
  183.                     UnitNum = client.ClientAddress.UnitNum
  184.                 }
  185.             };
  186.  
  187.             context.Suppliers.Add(supplier);
  188.             context.SaveChanges();
  189.  
  190.         }
  191.     }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment