I am using .NET Core MVC, and the situation goes like this.
I have a database, with a many to many relationship.
I have a "Courses" table. And i have a "Lecturers" table.
I also have a table "CourseLecturers" with composite key, which consists of 2 id's. LecturerId, and CoursesID.
I am using Entity Framework, and Model View Controller to show my data.
The model for my Courses
table looks like this :
public partial class Courses
{
[Key]
public int CourseId { get; set; }
public string CourseTitle { get; set; }
}
The model for my Lecturers
table looks similair :
public partial class Lecturers
{
[Key]
public int LecturerId { get; set; }
public string Lecturer { get; set; }
}
And the model for my CourseLecturers
table looks like this :
public partial class CourseLecturers
{
public int CourseId { get; set; }
public int LecturerId { get; set; }
}
The issue i have, or, what i want to do, is : i want to use my CourseLecturers controller, so that it will show CourseTitle, and Lecturer. Not CourseId and LecturerId. Is there any way i can access those attributes?
This is my Index
method in my CourseLecturers
controller :
public IActionResult Index()
{
/*
CourseLecturers courseLecturers = new CourseLecturers();
List<String> lista=new List<String>();
foreach (var item in _context.CourseLecturers)
{
lista.Add(_context.Courses.Where(c => c.CourseId == courseLecturers.CourseId).ToString());
}
*/
return View(_context.CourseLecturers.ToList());
}
I am wildly experimenting with controller, and this will return just the default integers and id's. (since i commented the code) I am not sure who to turn to. Is it even possible to have two strings appear, instead of integers, since my CourseLecturers model has two integers ? It seems foolish not to allow something like that. So any help i can get will be greatly appreciated.
After trying stuartd solution
Soo, this is my default context for CourseLecturers table :
modelBuilder.Entity<CourseLecturers>(entity =>
{
entity.HasKey(e => new { e.LecturerId, e.CourseId })
.HasName("PK_courseLecturers");
entity.ToTable("courseLecturers");
entity.Property(e => e.LecturerId).HasColumnName("lecturerID");
entity.Property(e => e.CourseId).HasColumnName("courseID");
});
Which throws build errors. The changes i made to that code were as follows :
modelBuilder.Entity<CourseLecturers>(entity =>
{
entity.HasKey(e => new { e.Lecturers.LecturerId, e.Courses.CourseId })
.HasName("PK_courseLecturers");
entity.ToTable("courseLecturers");
entity.Property(e => e.Lecturers.LecturerId).HasColumnName("lecturerID");
entity.Property(e => e.Courses.CourseId).HasColumnName("courseID");
});
Which is sintactically correct, but throws an exception : An exception of type 'System.ArgumentException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: The properties expression 'e => new <>f__AnonymousType1`2(LecturerId = e.Lecturers.LecturerId, CourseId = e.Courses.CourseId)' is not valid. The expression should represent a property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'.
So, there is that.