I am upgrading a project to EFCore 2.2.0 from 2.1.3 and encountered this bizarre issue. When loading data for a simple table, I get an exception that 'ExchangeCd1' is not defined. This column does not exist, but it is not referenced anywhere in the code. When I examine the mapping properties, I see it is listed as: {Property: ExchangeHoliday.ExchangeCd1 (no field, string) Shadow FK Index 2 2 2 0 1}
The classes are pretty simple, but the child has a composite primary key and the parent tries to reference it using its individual key.
public class Exchange
{
public string ExchangeCd { get; set; }
public string ExchangeName { get; set; }
public DayOfWeek WeekendStart { get; set; }
public DayOfWeek WeekendEnd { get; set; }
public ICollection<ExchangeHoliday> Holidays { get; set; }
}
public class ExchangeHoliday
{
public string ExchangeCd { get; set; }
public DateTime HolidayDate { get; set; }
public bool IsTradeHoliday { get; set; }
public bool IsSettleHoliday { get; set; }
}
I add some mapping, in order to define the primary key as a composite key
modelBuilder.Entity<Exchange>().HasKey(e => e.ExchangeCd);
modelBuilder.Entity<ExchangeHoliday>().HasKey(e => new { e.ExchangeCd, e.HolidayDate });
Then I execute a very simple select
var holidays = await dbContext.ExchangeHolidays.ToArrayAsync(cancellationToken).ConfigureAwait(false);
I turned on EFCore SQL logging, and it generates this for the SQL
SELECT [e].[exchange_cd], [e].[holiday_date], [e].[ExchangeCd1], [e].[is_settle_holiday], [e].[is_trade_holiday]
FROM [exchange_holiday] AS [e]
I tried to add explicit mapping to remove the shadow key, but it sticks around
modelBuilder.Entity<Exchange>()
.HasMany<ExchangeHoliday>()
.WithOne(e => e.Exchange)
.HasForeignKey(fk => fk.ExchangeCd);
Ok, so if I do the mapping on the child, it resolves the issue
modelBuilder.Entity<ExchangeHoliday>()
.HasOne<Exchange>()
.WithMany(e => e.Holidays)
.HasForeignKey(e => e.ExchangeCd);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.