dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.

Home Page:https://docs.microsoft.com/ef/ef6

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When having an inheritance hierarchy, EF creates a redundat foreign Key column.

Snarky36 opened this issue · comments

File a bug

I have an issue where i want to have a hierarchy of classes but EF creates a redundant column id even if I specified which one is the foreign Key.
I also tryed with fluent Api to explicitly tell the one to many relation ship.

Include your code

public abstract class PaymentDetail : BaseEntity
    {
        [ForeignKey("OrderId")]
        public Guid OrderId { get; set; }
        public decimal CustomerAmount { get; set; }
        public decimal ShopperAmount { get; set; }
        public abstract Dictionary<string, string> TypeDetails();
        public abstract string GetName();
    }
public class CreditCardPayment : PaymentDetail
    {
        public string LastFour { get; set; }
        public string Name { get; set; }
        public string CardType { get; set; }
        public DateTime ExpirationDate { get; set; }
        public string Token { get; set; }
        public string AuthorizationResult { get; set; }

        public override Dictionary<string, string> TypeDetails()
        {
            var details = new Dictionary<string, string>
            {
                { "Last Four", $"XXXX-XXXX-XXXX-{LastFour}" },
                { "Name", Name },
                { "Card Type", CardType },
                { "Exp. Date", $"{ExpirationDate.Month} / {ExpirationDate.Year}" }
            };
            return details;
        }
        public override string GetName()
        {
            return EnumHelpers.GetDisplayName(OrderPaymentType.CreditCard);
        }
    }

public class OnAccountPayment : PaymentDetail
    {
        public override Dictionary<string, string> TypeDetails()
        {
            var details = new Dictionary<string, string> { { "On Account", "Applied to customer." } };

            return details;
        }
        public override string GetName()
        {
            return EnumHelpers.GetDisplayName(OrderPaymentType.OnAccount);
        }
    }
 public class PayrollDeductPayment : PaymentDetail
    {
        public override Dictionary<string, string> TypeDetails()
        {
            var details = new Dictionary<string, string> { { "Payroll Deduct", "Amount will be deducted from Payroll." } };

            return details;
        }
        public override string GetName()
        {
            return EnumHelpers.GetDisplayName(OrderPaymentType.PayrollDeduct);
        }
    }```
    All the classes are the same they just implement the PaymentDetail.
And i have the order Class 

public class Order : BaseEntity
{
private ICollection _orderItems;
private ICollection _paymentDetails;

    public virtual Associate Associate { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual OrderShopper OrderShopper { get; set; }
    public virtual Store Store { get; set; }

    public virtual ICollection<OrderItem> Items => _orderItems != null ? _orderItems : (_orderItems = new HashSet<OrderItem>());
   
    public virtual ICollection<PaymentDetail> Payments => _paymentDetails != null ? _paymentDetails : (_paymentDetails = new HashSet<PaymentDetail>());

    public virtual ICollection<VoucherPayment> VoucherPayments => Payments.OfType<VoucherPayment>().ToList();

    public virtual ICollection<OverridePayment> OverridePayments => Payments.OfType<OverridePayment>().ToList();

    public virtual ICollection<PurchaseOrderPayment> PurchaseOrderPayments => Payments.OfType<PurchaseOrderPayment>().ToList();

    public virtual ICollection<CreditCardPayment> CreditCardPayments => Payments.OfType<CreditCardPayment>().ToList();

    public virtual ICollection<PayrollDeductPayment> PayrollDeductPayments => Payments.OfType<PayrollDeductPayment>().ToList();

    public virtual ICollection<OnAccountPayment> OnAccountPayments => Payments.OfType<OnAccountPayment>().ToList();
Configurations are the same for each class
 public class PaymentPayrollDeductEntityConfiguration : EntityTypeConfiguration<PayrollDeductPayment>
    {
        public PaymentPayrollDeductEntityConfiguration()
        {
            Map(m =>
            {
                m.MapInheritedProperties();
                m.ToTable("PaymentPayrollDeduct");
            });
        }
![image](https://github.com/dotnet/efcore/assets/74422129/e6801a70-d6a0-46e7-90e5-f9db8d4ac198)

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.