I asked this same question at stackoverflow. It's on hold and I fail to understand why, but it led me here to ask the same question. I'm hoping for a warmer reception here. First, my code.
public abstract class Entity
{
public class Room : Entity
{
public class Exit : Entity
{
My repository methods
public IEnumerable<Room> AllRoomsInclusive()
{
public void Update(Room room)
{
public void Save()
{
Here's the problem. My create view works just fine. I create a room, add exits, and the room and exits are inserted in to my database with no hitches. It's when I edit that I get stuck and I want to give up on Entity Framework and just go back to flat files for such a simple project. t I can't. This has to be so simple that I'm going to kick myself really hard when I see the solution.
Upon edit, I see the values in my post method are indeed the ones I selected from my edit screen. I call update and save in my repository and then do the normal thing of redirecting to my index page. However, all is not well. Changes to name and description on my room object will get updated in the database. Changes to the ToRoomId of any exit objects in the Exits collection of the room object do not. No errors are thrown. What am I missing or doing wrong?
public abstract class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}public class Room : Entity
{
public virtual ICollection<Exit> Exits { get; set; }
}public class Exit : Entity
{
// The room to which we belong
public int RoomId { get; set; }
public Room Room { get; set; }
// The room to which we connect
public int? ToRoomId { get; set; }
public Room ToRoom { get; set; }
}My repository methods
public IEnumerable<Room> AllRoomsInclusive()
{
var rooms = Context.Rooms.Include(r => r.Exits).ToList();
return rooms;
}public void Update(Room room)
{
Context.Entry(room).State = EntityState.Modified;
}public void Save()
{
Context.SaveChanges();
}Here's the problem. My create view works just fine. I create a room, add exits, and the room and exits are inserted in to my database with no hitches. It's when I edit that I get stuck and I want to give up on Entity Framework and just go back to flat files for such a simple project. t I can't. This has to be so simple that I'm going to kick myself really hard when I see the solution.
Upon edit, I see the values in my post method are indeed the ones I selected from my edit screen. I call update and save in my repository and then do the normal thing of redirecting to my index page. However, all is not well. Changes to name and description on my room object will get updated in the database. Changes to the ToRoomId of any exit objects in the Exits collection of the room object do not. No errors are thrown. What am I missing or doing wrong?