Why do I get an error if I have 1 dbContext with multiple dbSets when calling SaveChanges twice?

asp.net-mvc c# entity-framework

Question

EDIT

I'm new to MVC and Entity Framework.

  1. My first table (Participant) was created using the EF functionality Code First.
  2. The second table (Status) was created manually - directly in the DB + I wrote its Model class.
  3. Then I updated my DbContext class.

UPDATED CODE

public class Participant
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime RegistrationDate { get; set; }
    public string ClubName { get; set; }
    public string Email { get; set; }
    public string Weight { get; set; }
    public string BeltColor { get; set; }

    public virtual ICollection<Status> Statuses { get; set; }
}


public class Status
{
    public int Id { get; set; }
    public int Participant_Id { get; set; }
    public string Status_Text { get; set; }
    public DateTime Status_DateTime { get; set; }
}


public class BjjDBContext : DbContext
{
    public BjjDBContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Participant> Participants { get; set; }
    public DbSet<Status> Statuses { get; set; }
}

So after completing the steps above (tables, models, dbsets), I try to call the Create method in my Controller class. But I get a 'DbUpdateException was unhandled by user code'-exception when calling SaveChanges():

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Participant participant, Status status)
{
    if (ModelState.IsValid)
    {
        participant.RegistrationDate = DateTime.Now;
        db.Participants.Add(participant);
        db.SaveChanges();

        status.Participant_Id = participant.Id;
        status.Status_DateTime = DateTime.Now;
        status.Status_Text = "Registration Pending";

        db.Statuses.Add(status);
        db.SaveChanges(); // Where the DbUpdateException-exception happens

        return RedirectToAction("Index");
    }
    return View(participant);
}

Error description:

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Additional information: An error occurred while updating the entries. See the inner exception for details.

1
0
3/7/2014 1:19:25 PM

Accepted Answer

I had to update my database using Entity Framework (Code First), complete the following steps below.

Step 1:

I deleted my manually created tables, because Code First will create the tables automatically.

Step 2:

I selected TOOLS -> Library Package Manager -> Package Manager Console - to update my Database

enter image description here

Step 3:

I typed 'Update-Database' in the Console.

enter image description here

RESULT: My tables where created manually my DataBase was updated.

0
12/30/2013 7:25:04 AM

Popular Answer

The participant must exist in the database before you can save the "status". Add the "participant", save the changes, then add the "status" and save it. The dbContext is trying to match the reference to the "participant", but it isn't exist since you didn't save it before adding it as a references to the "status"



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow