none
Updating the database using MVC Entity Framework RRS feed

  • Question

  • Okay I am a newbie at MVC and Entity Framework. I am creating a website and have created a ViewModel and I need to update the database. I have a link that calls the following method

    public ActionResult AssignVolunteerToEvent(Guid eventId, Guid userId)
            {
                EventModel myEvent = db.EventModels.Find(eventId);
                myEvent.VolunteersAssignedTo.Add(userId);

                if (ModelState.IsValid)
                {
                    db.Entry(myEvent).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Details", "CompleteUser", new { Id = userId });
                }

                //Shouldn't get this far
                return View();
            }

    Everything runs fine with no errors but the database doesn't reflect the changes that I have made to the ViewModel. If I haven't provided enough information I apologize.

    • Moved by Bob Shen Monday, March 18, 2013 4:24 AM
    Friday, March 15, 2013 3:16 PM

Answers

  • Hi  Carl,

    The navigation property should be a collection type of VolunteerModel. Please check the following:

        public class EventModel
        {
            public Guid Id { get; set; }
    
            public virtual ICollection<VolunteerModel> VolunteerModels { get; set; }
        }
    
        public class VolunteerModel
        {
            public Guid Id { get; set; }
    
            public virtual ICollection<EventModel> EventModels { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<EventModel> EventModels { get; set; }
            public DbSet<VolunteerModel> VolunteerModels { get; set; }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 9:36 AM

All replies

  • Hi Carl,

    I would like to redirect you to appropriate forum for better responses.  


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 18, 2013 4:24 AM
  • Hi Carl,

    Welcome to the MSDN forum.

    Could you please share the table schema with us? Is the relationship between EventModel and Volunteers one to many?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 19, 2013 6:44 AM
  • The relationship is many to many. Each event can have multiple volunteers and each volunteer can be assigned to multiple events. I have no idea how to get you the schema.
    Tuesday, March 19, 2013 4:06 PM
  • I did a little checking and again I am new to MVC EF. In my model I have the VolunteerAssignedTo as a list of GUID. When I look in the SQL Server Object Explorer EF never created a column for the VolunteerAssignTo property. Which makes sense because I probably need some sore of Xref table. I know what the problem is but I don't know how to fix it.

    Tuesday, March 19, 2013 4:35 PM
  • Hi  Carl,

    The navigation property should be a collection type of VolunteerModel. Please check the following:

        public class EventModel
        {
            public Guid Id { get; set; }
    
            public virtual ICollection<VolunteerModel> VolunteerModels { get; set; }
        }
    
        public class VolunteerModel
        {
            public Guid Id { get; set; }
    
            public virtual ICollection<EventModel> EventModels { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<EventModel> EventModels { get; set; }
            public DbSet<VolunteerModel> VolunteerModels { get; set; }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 28, 2013 9:36 AM
  • Thank you Alexander this has fixed it. 
    Thursday, March 28, 2013 6:04 PM
  • Hi  Carl,

    If you have anything unclear, please feel free to let me know. I think next you could add volunteers to event via this:

    myEvent.VolunteerModels.Add(volunteerObject);

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 29, 2013 2:07 AM