Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:29:27+00:00 2026-06-01T04:29:27+00:00

When I try to EDIT a tennis court… I get this exception :System.Data.UpdateException; Unable

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:29:29+00:00Added an answer on June 1, 2026 at 4:29 am

    Set a breakpoint at db.Entry(tenniscourt).State = EntityState.Modified; have a look at what TennisClubID contains. 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I receive this message (see image below) when I try to edit in debugging.
I'm following this railscast and got stuck immediately: http://asciicasts.com/episodes/244-gravatar Whenever I try to edit
When I try to edit texts in my iPhone application ( UITextfield ), it
I am getting an error on my browser when i try to edit or
Edit: Solved. Hi, I'm starting with Qt, I try to connect a slot to
In a controller a error with Create/Edit ActionResult can be handled with a try-catch
Try as I might I cannot get my head around what the IteratorIterator class
Try as I might, I can't get a JNLP file to run locally (via
Why would my try-catch block still be throwing an error when it's handled? Exception
I try to edit ODT-documents programmaticly in PHP. In fact I just want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.