How do I stop EF Core creating a name like this? I have a table with students and workouts I have them setup for EF Core to make the link table as such. I'm using EF Core 5.09. I was following the book example
https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration
But I think something went wrong if it created a name like that
public class StudentClasses
{
public int StudentId { get; set; }
public Students Students { get; set; }
public int ClassId { get; set; }
public Classes Classes { get; set; }
}
Then in OnModelCreating
override, I have the following:
modelBuilder.Entity<StudentClasses>()
.HasKey(bc => new { bc.StudentId, bc.ClassId });
modelBuilder.Entity<StudentClasses>()
.HasOne(bc => bc.Students)
.WithMany(b => b.StudentClasses)
.HasForeignKey(bc => bc.StudentId);
modelBuilder.Entity<StudentClasses>()
.HasOne(bc => bc.Classes)
.WithMany(c => c.StudentClasses)
.HasForeignKey(bc => bc.ClassId);
But this results in a awful naming convention in the database.
How do I make it that it just shows ClassId
as the name of the foreign key I fixed the spelling mistake in code, but I don't know how to stop the classes before it. It's a one Student can belong to many classes relationship.
My Students class
public class Students
{
[Key]
public int StudentId { get; set; }
public int? Type { get; set; }
public string? FirstName { get; set; }
public string? Surname { get; set; }
public DateTime? DOB { get; set; }
public decimal? Weight { get; set; }
public decimal? Height { get; set; }
public int? Gender { get; set; }
public string? Age { get; set; }
public ICollection<StudentWorkouts> StudentWorkouts { get; set; }
public ICollection<StudentClasses> StudentClasses { get; set; }
public bool? IsDeleted { get; set; }
public bool? IsActive { get; set; }
public string? CreatedBy { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastUpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}
My Classes
class - no pun intended:
public class Classes
{
[Key]
public int ClassedId { get; set; }
public Guid? UserId { get; set; }
public ICollection<Students> Students { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public ICollection<StudentClasses> StudentClasses { get; set; }
public bool? IsDeleted { get; set; }
public bool? IsActive { get; set; }
public string? CreatedBy { get; set; }
public string? LastModifiedBy { get; set; }
public DateTime? LastUpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}