Could anyone explain to me a method for not getting the proxies from DbContext but the actual objects?
Here's a case:
public ActionResult PegarItem(int anuncio, String remetente, int destinatario)
{
var context = new ReuseContext();
Anuncio a = context.Anuncios.Find(anuncio);
Usuario d = context.Usuarios.Find(destinatario);
if (remetente == null)
{
return RedirectToAction("Login", "Account");
}
if (d.Name == User.Identity.Name)
{
return RedirectToAction("Index", new { error = 1 });
}
Usuario user = context.Usuarios.Where(b => b.email == remetente).FirstOrDefault();
var mensagem = new Mensagem(a, user, d);
MensagensController mc = new MensagensController();
context.Dispose();
mc.Create(mensagem);
return RedirectToAction("Index", new { success = 1 });
}
When it is done db.SaveChanges();
, it creates a new entity for the 2 users and the ad, I know this happens because of the proxies, but I do not know how to prevent this in C #.
public ActionResult Create([Bind(Include = "MensagemID,DataPostada,Remetente,Destinatario")] Mensagem mensagem)
{
if (ModelState.IsValid)
{
db.Mensagems.Add(mensagem);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mensagem);
}