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.TPH
- {
- [Table("Client", Schema = "TPH")]
- 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("Address", Schema = "TPH")]
- public abstract class Address : IAddress
- {
- [Key]
- 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; }
- int IAddress.Id { get => AddressId; }
- public abstract string AddressType { get; }
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- protected 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;
- }
- }
- public class ClientAddress : Address
- {
- public override string AddressType => typeof(ClientAddress).Name;
- public virtual Client? Client { get; set; }
- #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public ClientAddress() { }
- #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
- public ClientAddress(string name, string? unitNum, string streetNum, string streetName, string streetType, int suburbId, string city)
- : base(name, unitNum, streetNum, streetName, streetType, suburbId, city) { }
- }
- public interface IAddress
- {
- public int Id { get; }
- string Name { get; }
- string? UnitNum { get; }
- string StreetNum { get; }
- string StreetName { get; }
- string StreetType { get; }
- int SuburbId { get; }
- string City { get; }
- }
- public class TPHDbContext : DbContext
- {
- public DbSet<Client> Clients { get; set; }
- public DbSet<ClientAddress> ClientAddresses { 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", "TPH")
- .HasDiscriminator<string>("Discriminator")
- .HasValue<ClientAddress>(typeof(ClientAddress).Name);
- modelBuilder.Entity<Client>()
- .HasOne(x => x.ClientAddress)
- .WithOne(x => x.Client)
- .HasForeignKey<Client>("AddressId");
- }
- }
- }
- using EFCore6Tests.TPH;
- using Microsoft.EntityFrameworkCore;
- using NUnit.Framework;
- namespace EFCore6Tests
- {
- [TestFixture]
- public class TPHTests
- {
- [Test]
- public void CreateTest()
- {
- using var context = new TPHDbContext();
- 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 TPHDbContext();
- var clients = context.Clients.Include(x => x.ClientAddress).ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment