Stevepy

Many-to-One Example (Shared address)

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