System.Data.UpdateException;无法添加外键,因为主键';s值没有';t存在

本文关键字:存在 UpdateException Data 添加 System 因为 | 更新日期: 2023-09-27 18:29:18

当我试图编辑网球场时。。。我得到这个异常:System.Data.UpdateException;无法添加外键,因为主键值不存在

TennisCourt模型:

public class TennisCourt
{
    [Key]
    public int ID { get; set; }
    [Display(Name = "Extérieur ?")]
    [Column("Outside")]
    public bool Outside { get; set; }
    [Display(Name = "Disponible ?")]
    [Column("Available")]
    public bool Available { get; set; }
    [Display(Name = "Description")]
    [Column("Description")]
    [MaxLength(90, ErrorMessage = "Description cannot be longer than 90 characters.")]
    public string Description { get; set; }
    [Display(Name = "TennisClubID")]
    public int TennisClubID { get; set; }

    public virtual TennisClub TennisClub { get; set; }
    public virtual ICollection<Reservation> Reservations { get; set; } 
}

"TennisClub Model"

public class TennisClub
{
    public int ID { get; set; }
    [Required(ErrorMessage = "Nom du club requis.")]
    [Display(Name = "Nom")]
    [Column("Name")]
    [MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
    public string Name {get;set;}
    [Required(ErrorMessage = "Adresse du club requise.")]
    [Display(Name = "Adresse")]
    [Column("Address")]
    [MaxLength(40, ErrorMessage = "Address cannot be longer than 40 characters.")]
    public string Address {get;set;}
    [Required(ErrorMessage = "Ville requise.")]
    [Display(Name = "Ville")]
    [Column("City")]
    [MaxLength(20, ErrorMessage = "City cannot be longer than 20 characters.")]
    public string City { get; set; }
    [Required(ErrorMessage = "Numéro de téléphone requis.")]
    [Display(Name = "Num. Tél")]
    [Column("PhoneNumber")]
    [MaxLength(20, ErrorMessage = "Phone number cannot be longer than 20 characters.")]
    public string PhoneNumber { get; set; }
    [Display(Name = "Mail")]
    [Column("Mail")]
    [MaxLength(30, ErrorMessage = "Mail cannot be longer than 30 characters.")]
    public string Mail { get; set; }
    [Required(ErrorMessage = "Heure d'ouverture requise.")]
    [Display(Name = "Heure d'ouverture")]
    [Column("OpenTime")]
    [DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
    public DateTime OpenTime { get; set; }
    [Required(ErrorMessage = "Heure de fermeture requise.")]
    [Display(Name = "Heure de fermeture")]
    [Column("CloseTime")]
    [DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
    public DateTime CloseTime { get; set; }
    [Required(ErrorMessage = "Nombre d'heure(s) pour un match en simple requis.")]
    [Display(Name = "Temps de jeu (simple)")]
    [Column("GameTimeSimple")]
    [DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
    public DateTime GameTimeSimple { get; set; }
    [Required(ErrorMessage = "Nombre d'heure(s) pour un match en double requis.")]
    [Display(Name = "Temps de jeu (double)")]
    [Column("GameTimeDouble")]
    [DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
    public DateTime GameTimeDouble { get; set; }
    public virtual ICollection<TennisCourt> TennisCourts { get; set; }
    public virtual ICollection<Teacher> Teachers { get; set; }
    public virtual ICollection<Customer> Customers { get; set; }
    public virtual ICollection<Manager> Managers { get; set; }
    public virtual ICollection<Reservation> Reservations{ get; set; } 
}

错误发生在使用外键的TennisCourt和TennisClub之间。。。

奇怪的是,当我CREATE一个网球场时,它就工作了!

此处出现异常:

   [HttpPost]
    public ActionResult Edit(TennisCourt tenniscourt)
    {
        if (ModelState.IsValid)
        {
            try
            {
                db.Entry(tenniscourt).State = EntityState.Modified;
                db.SaveChanges();//here the exception
                return RedirectToAction("Index");
            }
            catch (DbUpdateException dbEx)
            {
                System.Diagnostics.Debug.WriteLine(dbEx.InnerException);
            }
        }
        return View(tenniscourt);
    }

请问我做错了什么?提前感谢。。。

System.Data.UpdateException;无法添加外键,因为主键';s值没有';t存在

db.Entry(tenniscourt).State = EntityState.Modified;设置断点看看TennisClubID包含什么。它可能被设置为数据库的网球俱乐部表中不存在的某个值。从你的例子中不容易看出原因。您的GUI中可能缺少与网球俱乐部有关的内容?