How to Mapping TPH in ownsmany

197 views Asked by At

I have three classes for abstract Payment.

public class CardPayment : Payment
{
    public CardPayment(Guid distributeId, Guid invoiceId, int userId, decimal amount, DateTime dateTime, long serialNumbner, long terminalNumber, long referenceNumber, DateTime bankDateTime) : base(distributeId, invoiceId, userId, amount, dateTime)
    public long SerialNumber { get;private set; }
    public long TerminalNumber { get; private set; }
    public long ReferenceNumber { get; set; }
    public DateTime BankDateTime { get; set; }
}

public  class Payment: EntityBase<Guid>
{
    public Guid DistributeId { get; private set; }
    public Guid InvoiceId { get; private set; }
    public int UserId { get; private set; }
    public decimal Amount { get; set; }
    public DateTime DateTime { get; set; }
    public  PaymentType Type { get; set; }
}

public class CashPayment : Payment
{
    public CashPayment(Guid distributeId, Guid invoiceId, int userId, decimal amount, DateTime dateTime) : base(distributeId, invoiceId, userId, amount,dateTime)
    {
    }
}


public class ChequePayment : Payment
{

    public string SerialNumber { get; private set; }
    public int BankId { get; private set; }
    public string BankBranchName { get; set; }
    public string NationalCode { get; set; }
    public string BankBranchCode { get; set; }
    public DateTime DueDate { get; set; }
    public string BankAccountNumber { get; set; }
    public string SayddiNumber { get; set; }

    
}

These show above.

I use ef-core. I want to use TPH for persistence.

 public class Invoice : EntityBase<Guid>, IAggregateRoot<Invoice>
{
    protected List<Payment> _payments = new List<Payment>();
    public IReadOnlyCollection<Payment> Payments => _payments.AsReadOnly();
}

On the other hand, I use the base class for mapping Aggregate roots like this.

public abstract class EntityMappingBase<TEntity> : IEntityTypeConfiguration<TEntity>
    where TEntity : class,IEntityBase, IAggregateRoot<TEntity>
{

    public abstract void Configure(EntityTypeBuilder<TEntity> builder);
}

}

   public class InvoiceMapping : EntityMappingBase<Invoice>
    {
        public override void Configure(EntityTypeBuilder<Invoice> builder)
        {
            Initial(builder);


            builder.OwnsMany(a => a.Payments, map =>
            {
                map.Property(i => i.Id)
               .ValueGeneratedNever();
                map.HasKey(i => i.Id);

                map.WithOwner().HasForeignKey("InvoiceId");

                map.UsePropertyAccessMode(PropertyAccessMode.Field);


                ToTable(map);
            });

        }
    }

But I don't map these classes into The Invoice Class.

0

There are 0 answers