Given the following two models:
public class Card
{
public int CardId { get; set; }
public Media Media { get; set; }
}
public class Media
{
public int MediaId { get; set; }
[Required]
public string FileName { get; set; }
}
And the following controller method:
[HttpPost]
public ActionResult Create(Card card)
{
db.Media.Attach(card.Media);
ModelState.Remove("Media.FileName");
if (ModelState.IsValid)
{
db.Cards.Add(card);
db.SaveChanges();
}
return JsonNetSerializedResult(card);
}
I want to create a new Card, but associate it with an existing Media object. I POST to "Controller/Create" and include a "Media.MediaId" parameter that contains the ID of the existing Media record and let EF make the association.
However, after SaveChanges() is called, the only property updated on the Card instance is the CardId. I also need to retrieve the Media.FileName and set that property on the Card instance.
Is there a generic way to tell EF that I want return the updated Card data, and to also return the associated Media data commiting the data?