I'm building a Mobile MVC application and stumbled across many problems but this is bugging me a lot.
This is a Message class i'm reffering to:
public class Message
{
public int MessageID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Recieved { get; set; }
[ForeignKey("User")]
public int AuthorUserID { get; set; }
//P\\ Navigation properties
public virtual ICollection<MessageRecipient> MessageRecipients { get; set; }
public virtual User User { get; set; }
}
I create the new Message object and save it in database.
messageService.AddMessage(newMessage);
Message freshMessage = messageService.GetNewestMessage();
At this point it has no Recipients attached. User Gets redirected to a View where (s)he can add recipients to a message.
Whe the user add the recipient i invoke the method through ajax that creates the MessageRecipient object and adds it to the Recipients collection of the message.
message.MessageRecipients.Add(recipient);
The ideal situation would be: when the user finished adding recipients and goes back to the Edit View, added recipients are visible.
When Edit action is being invoked the GetMessage method is invoked
Message message = messageService.GetMessage(id);
Because this message has been retrieved before it would be held in ObjectContext, so EF would serve me the entity from the memory rather than query DB for fresh one and this is what i want to happen. I do not know how to save the changes in that ObjectContext without saving changes to Db by invoking SaveChanges().
So my question is: Is there a way to change the state of the entity held in a memory so next time when it's been queried for i get the entity with the changes i have made to it?
I just want to work with the object and save it to Db when i've done with it rather than make a trip to Db each time user adds a recipient to a message.
I'm new to .NET and EF...
Any help would be appreciated.