1
Could someone explain me a method not to pick up the proxies from the Dbcontext and yes the real objects?
Following 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 performed db.SaveChanges();
, it creates a new entity for the 2 users and the ad, I know this occurs 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);
}
thank you for the reply, now I understand more the difference of context of my system
– jbrunoxd