0

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);
}
17
  • 3
    Have you debugged the code? are you sure that ModelState.IsValid is true? Commented Jul 1, 2016 at 12:33
  • Your question is unclear... where in this method does the program error out? Commented Jul 1, 2016 at 12:37
  • The code runs smoothly (no error msg) - it creates the lending but it doesn't save the changes made to the reserves. So to be clear - it doesn't set "served" to true in the database. Commented Jul 1, 2016 at 12:41
  • so then it is not entering the foreach loop? have you debugged to find out why? Does your list have any objects inside of it? Commented Jul 1, 2016 at 12:42
  • 1
    you are sure that the records being stored in that list are what you are expecting? Commented Jul 1, 2016 at 13:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

You are right - i changed it, but it didn't solve the problem. I didn't know if the EntitiyState.Modified was necessary. Seems it doesn't work with or without it.
Can you write down the reserve ids, next time you debug and check them in the database manually, before your run anything after save changes, to make sure there isn't a function else where in the code that is switching it back
Reserve IDs Reserve IDs DB I checked them right after savechanges - i uploaded two screenshots so you can see.
Try the links in my previous comment
@Mbrzeske It says they're false in the code is that before or after saving?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.