When I try to EDIT a tennis court… I get this exception :System.Data.UpdateException; Unable to add foreign key because primary key’value doesn’t exists
TennisCourt model:
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; }
}
The error is between classes TennisCourt and TennisClub with the foreign key…
Bizarrely, when I CREATE a tennis court, it works !
Exception here:
[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);
}
What I’m doing wrong please ? Thanks in advance…
Set a breakpoint at
db.Entry(tenniscourt).State = EntityState.Modified;have a look at whatTennisClubIDcontains. It probably is set to some value that doesn’t exist in the Tennis club table of the database. From your example it is not easy to see why. Mabe something refernce to tennisclub is missing in your GUI?