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.TPC
- {
- [Table("Client", Schema = "TPC")]
- 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("ClientAddress", Schema = "TPC")]
- public class ClientAddress : IAddress
- {
- [Key]
- public int ClientAddressId { 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 => ClientAddressId; }
- string IAddress.AddressType
- {
- get => typeof(ClientAddress).Name;
- }
- public 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() { } // make protected if only for EF to use.
- #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)
- {
- Name = name;
- UnitNum = unitNum;
- StreetNum = streetNum;
- StreetName = streetName;
- StreetType = streetType;
- SuburbId = suburbId;
- City = city;
- }
- }
- 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 TPCDbContext : 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<Client>()
- .HasOne(x => x.ClientAddress)
- .WithOne(x => x.Client)
- .HasForeignKey<Client>("ClientAddressId");
- }
- }
- }
- using EFCore6Tests.TPC;
- using Microsoft.EntityFrameworkCore;
- using NUnit.Framework;
- namespace EFCore6Tests
- {
- [TestFixture]
- public class TPCTests
- {
- [Test]
- public void CreateTest()
- {
- using var context = new TPCDbContext();
- 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 TPCDbContext();
- var clients = context.Clients.Include(x => x.ClientAddress).ToList();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment