i have 3 table. 2 of them has got many to many relations. Last one is secondary table like that:

Student:
id------- Name------- SurName
1-------- fsfsf---------- fsfsfsdf
2-------- dfdsf -------- sfsfsdfsdf

Course

id----------Name-------
11-------- course1----------
22-------- course2--------
23-------- course3--------


StudentCourse

Studentid---------CourseId
1-------------------11
1-------------------12
2-------------------22
2-------------------23
2-------------------11

But Secondary table is hidden inside of the Entity framework model. But i need to add Studentid and Courseid without any changes of Course and Student table. How can i achive that?

有帮助吗?

解决方案

You can use Fluent-API to map your relation table. To achieve that, you have to edit the OnModelCreating method like this:

modelBuilder.Entity<Course>()
   .HasMany<Student>(c => c.Students)
   .WithMany(s => s.Courses)
   .Map(m => {
       m.ToTable("StudentCourses");
       m.MapLeftKey("CourseId");
       m.MapRightKey("StudentId");
    });

Don't forget to do an "update-database" command on your Package Manager.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top