Mbrzeske
Mbrzeske

Reputation: 19

C# mvc project db.savechanges() not updating

I have a problem concerning the following code snippet for my university project. The system this is implemented in is a library for books - nothing too special as i am new to EF and MVC. The following is the [Http-Post] Method of creating a lending. When a lending is created, i want to make sure all reserves concerning the same User and the same Medium will be marked as served so they won't play a role for the next lending.

My problem is, that it won't save the changes the reserve-item. Does anybody see a reason why? It's not the only place i change values, but i'm not able to find out whats different here.

Model

public partial class Reserve
{
    public int Id { get; set; }
    public System.DateTime ReserveDate { get; set; }
    public string User { get; set; }
    public bool Served { get; set; }
    public virtual Medium Medium { get; set; }
}

Code

public ActionResult Create([Bind(Include = "Id,LendingDate,ExpiryDate,ReturnDate,User,Copy")] Lending lending, int? Copy_ID)
{
    lending.Copy = db.CopySet.Find(Copy_ID);
    lending.LendingDate = DateTime.Today;
    lending.ExpiryDate = lending.LendingDate.AddDays(DaysToExpiry);
    lending.ReturnDate = null;

    if (ModelState.IsValid)
    {
        //wenn ausleihe erfolgt, die Reservierung finden, die zu Benutzer und Medium passt
        List<Reserve> resList = db.ReserveSet.Where(x => x.User.Contains(lending.User) && x.Medium.Id.Equals(lending.Copy.Medium.Id)).ToList();

        //für alle Elemente 
        foreach (Reserve r in resList)
        {
            Reserve res =  db.ReserveSet.Find(r.Id);
            res.Served = true;
            db.Entry(res).State = EntityState.Modified;

            db.SaveChanges();
        }
        db.LendingSet.Add(lending);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(lending);
}

Upvotes: 0

Views: 141

Answers (1)

johnny 5
johnny 5

Reputation: 21033

I don't know what will happen when you do this. Your pulling the entity out from your reserve set twice, which can mess up change tracking. This code is redundant, you already have the items you need there is not reason to call find if your just setting all of the served to true. Also your call to save changes is going to cause excess communication between sql and your application, if for some reason one of you items fails to save then you will have partially saved data just call it once at then end instead.

List<Reserve> resList = db.ReserveSet.Where(x => x.User.Contains(lending.User) 
    && x.Medium.Id.Equals(lending.Copy.Medium.Id)).ToList();

//für alle Elemente 
foreach (Reserve r in resList)
{
    Reserve res =  db.ReserveSet.Find(r.Id);
    res.Served = true;
    db.Entry(res).State = EntityState.Modified;

    db.SaveChanges();
}

You can just do this instead:

List<Reserve> resList = db.ReserveSet.Where(x => x.User.Contains(lending.User)
     && x.Medium.Id.Equals(lending.Copy.Medium.Id)).ToList();

foreach(var res in resList)
{
   res.Served = true;
}    

db.SaveChanges();

I'm not sure if that will fix you issue but you can start there.

Upvotes: 1

Related Questions