problem with dynamic proxies ASP .NET MVC

1

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);
    }
    
asked by anonymous 12.11.2015 / 23:33

1 answer

1

Well, you're using the wrong technology. So you have these results.

These two lines make no sense:

MensagensController mc = new MensagensController();
context.Dispose();

First, Controllers are instantiated by the framework . Second because you are using a Controller as a Helper to not have the problem of repeating logic, only a Controller returns a% , which is part of the cycle of a request, not a block of logic, which is what you are doing. Third because you are using two different contexts with chained logic, and the Entity Framework understands that they are new entities instead of existing ones.

To solve, isolate your messaging logic in a static class:

public static class MensagensHelper 
{
    public static void CriarMensagem(Mensagem mensagem, DbContext contexto)
    {
        contexto.Mensagems.Add(mensagem);
        contexto.SaveChanges();
    }
}

Modify your Action on Controller to the following:

public ActionResult PegarItem(int anuncio, String remetente, int destinatario)
{
     using (var context = new ReuseContext()) 
     {
         var a = context.Anuncios.Find(anuncio);
         var d = context.Usuarios.Find(destinatario);

         if (remetente == null)
         {
             return RedirectToAction("Login", "Account");
         }

         if (d.Name == User.Identity.Name)
         {
             return RedirectToAction("Index", new { error = 1 });
         }

         var user = context.Usuarios.FirstOrDefault(b => b.email == remetente);
         MensagensHelper.CriarMensagem(new Mensagem(a, user, d), context);
         return RedirectToAction("Index", new { success = 1 });
     }
}
    
13.11.2015 / 14:45