简体   繁体   English

根据文档,EF Core 5 多对多关系无法正常工作

[英]EF Core 5 Many To Many relationships not working as should according to docs

According to the documentation , EF Core 5.0 supports many-to-many relationships without explicitly mapping the join table.根据文档,EF Core 5.0 支持多对多关系,无需显式映射连接表。

I'm having issues with the below in EF Core 5.0.9:我在 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();
}

I have included Students as part of the query.我已将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 packages: Nuget 包:

在此处输入图像描述

SQL database: SQL 数据库:

在此处输入图像描述

When I inspect the booking table, its empty does EF not automatically add into BookingStudent if so how do I tell ef core to add in the student?当我检查预订表时,它是空的,EF 不会自动添加到 BookingStudent 中吗?如果是这样,我如何告诉 ef core 添加学生? I have saved their StudentId into booking so why has it not created an entry.我已将他们的 StudentId 保存到预订中,为什么它没有创建一个条目。

It Created the booking table entry它创建了预订表条目

在此处输入图像描述

In My head I should see the values 10 and 72 in booking student should I not.在我的脑海中,我应该在 booking student 中看到值 10 和 72,但我不应该这样做。

But not the booking Student Entry?.但不是预订学生条目? According to the docs it creates it automatically yeah but what about saving entries does it not still do that automatically.根据文档,它会自动创建它,是的,但是关于保存条目呢,它仍然不会自动执行。

在此处输入图像描述

At this stage I have not placed anything in my DBContext on create method as the docs say u dont need that any more???在这个阶段,我没有在我的 DBContext on create 方法中放置任何东西,因为文档说你不再需要它了???

This is how I save my student.我就是这样拯救我的学生的。 As you see am filling in the foreign key of StudentId.如您所见,我正在填写 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");

My Add to booking which the api is calling in the web api is. api 在web api 中呼叫我的预订。

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

Edit 2编辑 2

Is this what u mean这是你的意思吗

//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;
}

If it's a many-to-many relationship, why do you have a StudentId property on Booking ?如果是多对多关系,为什么你在Booking上有一个StudentId属性?

Remove the property first:先删除属性:

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; }
}

And then add the student you want (which will have an ID) to the ICollection of students you have for a booking.然后将您想要的学生(将有一个 ID)添加到您要预订的学生的ICollection中。

booking.Students.Add(student);

That will trigger the EF many-many relationship.这将触发 EF 多对多关系。

Something like this:是这样的:

var student = new Student { ... };

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

await api.AddToBooking(newBooking);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM