'EF Core creates duplicate foreign key
I'm using EF Core 6 and MS SQL Server. EF Core creates a redundant foreign key in my case.
There are my entities:
public class CashFlow : EntityInfo
{
public CompanyOverview CompanyOverview { get; set; }
public List<CashFlowReport> AnnualReports { get; set; }
public List<CashFlowReport> QuarterlyReports { get; set; }
}
public class CashFlowReport : Report
{
// Many string properties
}
And base class Report:
public abstract class Report
{
[Key]
public int Id { get; set; }
public string Symbol { get; set; }
public string FiscalDateEnding { get; set; }
public string ReportedCurrency { get; set; }
}
I've tried to configure it like this:
public override void Configure(EntityTypeBuilder<BalanceSheet> modelBuilder)
{
base.Configure(modelBuilder);
modelBuilder.HasMany(x => x.AnnualReports)
.WithOne()
.HasForeignKey(x => x.Symbol);
modelBuilder.HasMany(x => x.QuarterlyReports)
.WithOne()
.HasForeignKey(x => x.Symbol);
}
But EF Core created two foreign keys in BalanceSheetReport table:
BalanceSheetSymbol nvarchar(450)
constraint FK_BalanceSheetReport_balance_sheet_BalanceSheetSymbol
references u1598429_u1598429.balance_sheet,
Symbol nvarchar(450)
constraint FK_BalanceSheetReport_balance_sheet_Symbol
references u1598429_u1598429.balance_sheet,
What do I need to do to remove FK_BalanceSheetReport_balance_sheet_BalanceSheetSymbol?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
