首页 > 解决方案 > 根据文档,EF Core 5 多对多关系无法正常工作

问题描述

根据文档,EF Core 5.0 支持多对多关系,无需显式映射连接表。

我在 EF Core 5.0.9 中遇到以下问题:

public List<Booking> GetAllBookingsForDay(int day, DateTime bookingDate)
{
     return db.Bookings
    .Where(w => w.IsActive == true & w.IsDeleted == false & w.DayOfWeek == day & w.Time >= bookingDate)
    .Include(c => c.Students)
    .ToList();
}

我已将其Students作为查询的一部分。

Booking.cs

public class Booking
{
    public int Id { get; set; }
    public int? DayOfWeek { get; set; }
    public DateTime? BookingDate { get; set; }
    public bool? IsAbsent { get; set; }
    public DateTime? Time { get; set; }
    public bool? HasCheckedIn { get; set; }
    public ICollection<Student> Students { get; set; }  // notice this
    public int? StudentId { get; set; }
    public bool? IsDeleted { get; set; }
    public bool? IsActive { get; set; }
    public string? CreatedBy { get; set; }
    public string? LastModifiedBy { get; set; }
}

Student.cs

public class Student
{   
    public int Id { get; set; }
    public int? Type { get; set; }
    public int? TeamId { 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? Photo { get; set; }
    public int? Age { get; set; }
    public ICollection<Booking> Bookings { get; set; } // notice here my link back 
     to bookings
   ...
 }

Nuget 包:

在此处输入图像描述

SQL 数据库:

在此处输入图像描述

当我检查预订表时,EF不会自动添加到BookingStudent中,如果是这样,我该如何告诉ef core添加到学生中?我已将他们的 StudentId 保存到预订中,为什么它没有创建条目。

它创建了预订表条目

在此处输入图像描述

如果我不应该在我的脑海中看到预定学生的值 10 和 72。

但不是预订学生入境?根据文档,它会自动创建它,是的,但是保存条目呢,它仍然不会自动执行。

在此处输入图像描述

在这个阶段,我没有在我的 DBContext on create 方法中放置任何东西,因为文档说你不再需要它了???

这就是我拯救我的学生的方式。如您所见,我正在填写 StudentId 的外键。

Booking newBooking = new Booking();
newBooking.DayOfWeek = DayNumber;
newBooking.BookingDate = BookingDate;
newBooking.Time=Helpers.Dates.GetDateZeroTime(selectedBookingDate.Date).Add(timePicker.Time);
newBooking.StudentId = StudentId;
newBooking.HasCheckedIn = false;
newBooking.IsActive = true;
newBooking.IsDeleted = false;
newBooking.IsAbsent = false;
await api.AddToBooking(newBooking);
await DisplayAlert(Constants.AppName, "Booking Created For Student", "OK");

我在 web api 中调用的 api 的 Add to booking 是。

public Booking AddBooking(Booking  booking)
{
    db.Bookings.Add(booking);
    db.SaveChanges();
    return booking;
}

编辑 2

这就是你的意思吗

//Do i need to put the second param in for Student here ?
public Booking AddBooking(Booking booking, Student student)
{
        booking.Students.Add(student);
        db.Bookings.Add(booking);

        db.SaveChanges();
        return booking;
}

标签: c#entity-frameworkentity-framework-core

解决方案


如果是多对多关系,你为什么要在StudentId上拥有一个属性Booking

首先删除属性:

public class Booking
{
    public int Id { get; set; }
    public int? DayOfWeek { get; set; }
    public DateTime? BookingDate { get; set; }
    public bool? IsAbsent { get; set; }
    public DateTime? Time { get; set; }
    public bool? HasCheckedIn { get; set; }
    public ICollection<Student> Students { get; set; }
    public bool? IsDeleted { get; set; }
    public bool? IsActive { get; set; }
    public string? CreatedBy { get; set; }
    public string? LastModifiedBy { get; set; }
}

然后将您想要的学生(将有一个 ID)添加到ICollection您预订的学生中。

booking.Students.Add(student);

这将触发 EF 多对多关系。

像这样的东西:

var student = new Student { ... };

var newBooking = new Booking {
    Student = new List<Student> { student },
    ...
};

await api.AddToBooking(newBooking);

推荐阅读