EF Core 7 not using my compound keys and throws error
Banging my head on this one. I'm trying to upgrade to Entity Framework Core 7. My class has a YearTerm and a CourseId. I can't get EF to use my key annotations or fluent API to identify my keys. I've tried the line below as in the Microsoft documents. It's a legacy table so I can't change it.
I've also tried removing the annotation and using Fluent API. That line is also below and I made sure I only used one or the other at the same time.
[PrimaryKey(nameof(YearTerm), nameof(CourseId))]
public class CourseInstance
{
[Column("YRTR")]
public string YearTerm { get; set; }
[Column("COU_ID")]
public string CourseId { get; set; }
.....
[ForeignKey("YearTerm")]
public YRTRCal YearTearmRecord { get; set; }
}
modelBuilder.Entity<CourseInstance>()
.HasKey(c => new { c.YearTerm, c.CourseId });
The error I get is :
Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'CourseInstanceCourseId'.
Invalid column name 'CourseInstanceYearTerm'.'
I'm assuming it's not seeing my primary key columns so it's trying to use the column names it thinks you should have and of course they don't exist either in my table or in my model.
Any ideas?
Thanks;
Tried both the annotations and Fluent API and I expected it to work.