Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using System.ComponentModel.DataAnnotations;
- using System.ComponentModel.DataAnnotations.Schema;
- namespace EFCore6Tests.TPT
- {
- [Table("Client", Schema = "TPT")]
- public class Client
- {
- [Key]
- public int ClientId { get; protected set; }
- public string Name { get; set; }
- public virtual ClientAddress ClientAddress { get; set; }
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public Client() { }
- #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- }
- [Table("Supplier", Schema = "TPT")]
- public class Supplier
- {
- [Key]
- public int SupplierId { get; protected set; }
- public string Name { get; set; }
- public virtual SupplierAddress SupplierAddress { get; set; }
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public Supplier() { }
- #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- }
- [Table("Address", Schema = "TPT")]
- public abstract class Address
- {
- [Key]
- [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
- public int AddressId { get; protected set; }
- public string Name { get; set; }
- public string? UnitNum { get; set; }
- public string StreetNum { get; set; }
- public string StreetName { get; set; }
- public string StreetType { get; set; }
- public int SuburbId { get; set; }
- public string City { get; set; }
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public Address() { }
- #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public Address(string name, string? unitNum, string streetNum, string streetName, string streetType, int suburbId, string city)
- {
- Name = name;
- UnitNum = unitNum;
- StreetNum = streetNum;
- StreetName = streetName;
- StreetType = streetType;
- SuburbId = suburbId;
- City = city;
- }
- }
- [Table("ClientAddress", Schema = "TPT")]
- public class ClientAddress : Address, IAddress
- {
- int IAddress.Id { get => AddressId; }
- string IAddress.AddressType
- {
- get => typeof(ClientAddress).Name;
- }
- public virtual Client? Client { get; set; }
- }
- [Table("SupplierAddress", Schema = "TPT")]
- public class SupplierAddress : Address, IAddress
- {
- int IAddress.Id { get => AddressId; }
- string IAddress.AddressType
- {
- get => typeof(SupplierAddress).Name;
- }
- public virtual Supplier? Supplier { get; set; }
- }
- public interface IAddress
- {
- public int Id { get; }
- string AddressType { get; }
- string Name { get; }
- string? UnitNum { get; }
- string StreetNum { get; }
- string StreetName { get; }
- string StreetType { get; }
- int SuburbId { get; }
- string City { get; }
- }
- public class TPTDbContext : DbContext
- {
- public DbSet<Client> Clients { get; set; }
- public DbSet<Supplier> Suppliers { get; set; }
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlServer(@"Data Source=Machine\SQLEXPRESS;Initial Catalog=Inheritance;Trusted_Connection=Yes;MultipleActiveResultSets=True;TrustServerCertificate=True");
- base.OnConfiguring(optionsBuilder);
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- modelBuilder.Entity<Address>()
- .ToTable("Address", "TPT");
- modelBuilder.Entity<Client>()
- .HasOne(x => x.ClientAddress)
- .WithOne(x => x.Client)
- .HasForeignKey<Client>("ClientAddressId");
- modelBuilder.Entity<Supplier>()
- .HasOne(x => x.SupplierAddress)
- .WithOne(x => x.Supplier)
- .HasForeignKey<Supplier>("SupplierAddressId");
- }
- }
- }
- using EFCore6Tests.TPT;
- using Microsoft.EntityFrameworkCore;
- using NUnit.Framework;
- namespace EFCore6Tests
- {
- [TestFixture]
- public class TPTTests
- {
- [Test]
- public void CreateTest()
- {
- using var context = new TPTDbContext();
- var client = new Client
- {
- Name = "Test1",
- ClientAddress = new ClientAddress { Name = "Test", StreetNum = "1a", StreetName = "Pelican", SuburbId = 2, StreetType = "Drive", City = "Bay City" }
- };
- context.Clients.Add(client);
- context.SaveChanges();
- }
- [Test]
- public void ReadTest()
- {
- using var context = new TPTDbContext();
- var clients = context.Clients.Include(x => x.ClientAddress).ToList();
- }
- [Test]
- public void AssociateAddressToSupplier()
- {
- using var context = new TPTDbContext();
- var client = context.Clients.Include(x => x.ClientAddress).First();
- var supplier = new Supplier
- {
- Name = "Test",
- SupplierAddress = new SupplierAddress
- {
- Name = client.ClientAddress.Name,
- StreetName = client.ClientAddress.StreetName,
- StreetNum = client.ClientAddress.StreetNum,
- StreetType = client.ClientAddress.StreetType,
- SuburbId = client.ClientAddress.SuburbId,
- City = client.ClientAddress.City,
- UnitNum = client.ClientAddress.UnitNum
- }
- };
- context.Suppliers.Add(supplier);
- context.SaveChanges();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment