c# - 根据文档,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;
}
解决方案
如果是多对多关系,你为什么要在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);
推荐阅读
- go - 在从 yaml 文件读取的 go 结构中模拟类似补丁的合并
- c# - 编辑gridview记录页面导航后如何保留文本框值和gridview值
- sql - 在 MariaDB 10.2.15 中使用 COUNT CASE WHEN MONTH 语句
- angular - 错误 TS1055:类型“布尔”不是 Ionic 4 中 ES5/ES3 中的有效异步函数返回类型
- wso2 - 使用 wsdl2java 调用 wso2 管理服务
- vue.js - 如何在 vuejs 中使用自定义指令更改字体大小
- r - 如何重新计算列并将新值存储在同一数据框中?
- php - 如何反转一组逗号分隔的字符串并在 PHP 中以原始顺序返回它们?
- mysql - 数据建模和实现关系[1对多][多对多]等
- node.js - 我可以在不安装 Express 的情况下使用 body-parser 模块吗?