problem with Dynamic proxies ASP . NET MVC

Asked

Viewed 109 times

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);
    }

1 answer

1


Well, you’re using the wrong technology. That’s why you have these results.

Those two lines don’t make any sense:

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

First because Controllers are instantiated by the framework. Second because you’re wearing one Controller as a Helper not to have the problem of repetition of logic, only that a Controller return a ActionResult, which is part of a request cycle, not a logic block, which is what you’re doing. Third because you are using two different contexts with chained logic, and the Entity Framework understands that they are new entities rather than existing ones.

To resolve, isolate your message creation 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 in the 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 });
     }
}
  • 1

    thank you for the reply, now I understand more the difference of context of my system

Browser other questions tagged

You are not signed in. Login or sign up in order to post.