Stevepy

TPC Example

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