【问题标题】:EF Core - Add One to many recordsEF Core - 添加一对多记录
【发布时间】:2020-11-19 10:51:19
【问题描述】:

拥有基数为 1 的模型 CinemaHall 0..n

Cinema.cs

public class Cinema
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int CinemaId { get; set; }

    [JsonPropertyName("name")]
    [Required]
    public string Name { get; set; }

    [JsonPropertyName("city")]
    [Required]
    public string City { get; set; }

    [JsonPropertyName("halls")]
    [Required]
    public List<Hall> Halls { get; set; } = new List<Hall();
}

Hall.cs

public class Hall
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int HallId { get; set; }
    public string Name { get; set; }
    public uint Capacity { get; set; }
    public Cinema Cinema { get; set; }
    public int Rows { get; set; }
    public List<Seat> Seats { get; set; } = new List<Seat>();
    public List<MovieShow> Shows { get; set; } = new List<MovieShow>();
}

CinemaRepository.cs

public async Task<Hall> AddHall(int cinemaId, Hall hall) {
  var cinema = await _context.Cinemas.FirstOrDefaultAsync(c => c.CinemaId == cinemaId);
  if (cinema == null) { return null; }
  var hallFound = cinema.Halls.FirstOrDefault(cinema => cinema.HallId == hall.HallId);
  if (hallFound != null) { return null; }
  hall.Cinema = cinema;
  await _context.Halls.AddAsync(hall);
  cinema.Halls.Add(hall);
  await _context.SaveChangesAsync();
  return hall;
}

但这只会将hall 添加到Halls,其 Cinema 属性为空。

【问题讨论】:

    标签: c# database asp.net-core entity-framework-core one-to-many


    【解决方案1】:

    您应该将外键添加到您的 Hall 类并修改您的 Hall 如下:

        public class Hall
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int HallId { get; set; }
        public string Name { get; set; }
        public uint Capacity { get; set; }
        public int CinemaId { get;set;}
        public Cinema Cinema { get; set; }
        public int Rows { get; set; }
        public List<Seat> Seats { get; set; } = new List<Seat>();
        public List<MovieShow> Shows { get; set; } = new List<MovieShow>();
    }
    

    然后在你的CinemaRepository:

     public async Task<Hall> AddHall(int cinemaId, Hall hall)
            {
                var cinema = _context.Cinemas.Include(c => c.Halls).FirstOrDefault(c => c.CinemaId == cinemaId);
                if (cinema == null) { return null; }
                var hallFound = cinema.Halls.FirstOrDefault(cinema => cinema.HallId == hall.HallId);
                if (hallFound != null) { return null; }
                cinema.Halls.Add(hall);
                await _context.SaveChangesAsync();
                return hall;
            }
    

    【讨论】:

    • 感谢您的回答。我目前正试图通过说“SQLite 错误 19:'FOREIGN KEY 约束失败'”来弄清楚为什么 EF Core 不想更新数据库。
    • 因为你查询的电影院应该包含Halls,否则数据库不会更新,所以你应该使用Include来查询。
    • 我刚刚将 int Id 作为外键添加到所有表中,然后运行 ​​Add-Migration & Update-Database 并且发生了这种情况。在回购中,我的代码与您的答案完全相同
    • 按照提供给我的方式将它添加到您的大厅。
    • 相当长。好吧,我不知道它是怎么发生的,但是 GET /halls 开始重新调整错误消息“System.InvalidOperationException:数据在序号 2 处为 NULL。不能在 NULL 值上调用此方法。在调用之前使用 IsDBNull 检查。”这就是为什么我想删除表记录并尝试删除所有记录,但它返回相同的消息。
    猜你喜欢
    • 2021-09-15
    • 1970-01-01
    • 2021-04-23
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2018-11-08
    • 2023-03-17
    相关资源
    最近更新 更多