0

I'm new using entity framework, and I'm trying to insert into the DB.
But I'm having an issue, because I need to only SaveChanges from objects of other 3 scopes.
Like this:
These are my three Actions that Add the objects into my entities:

public void AddEndereco(entidade_endereco entEndereco)
{
    db.entidade_endereco.Add(entEndereco);
}

public void addContato(entidade_contato entContato)
{
    db.entidade_contato.Add(entContato);
}

public void addBanco(entidade_banco entBanco)
{
    db.entidade_banco.Add(entBanco);
}

And in this action I need to insert all the objects into my DB:

   [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(entidade entidade, string Grupo, string Situacao)
        {
            if (ModelState.IsValid)
            {
                if (Grupo != "")
                    entidade.gre_codigo = Convert.ToInt32(Grupo);
                if (Situacao != "")
                    entidade.sie_codigo = Convert.ToInt32(Situacao);
                if (entidade.ver_ativo)
                    entidade.ent_ativo = "S";
                else
                    entidade.ent_ativo = "N";

                if (entidade.ver_cliente)
                    entidade.ent_cliente = "S";
                else
                    entidade.ent_cliente = "N";

                if (entidade.ver_fornecedor)
                    entidade.ent_fornecedor = "S";
                else
                    entidade.ent_fornecedor = "N";

                //ADDING ANOTHER OBJECT
                db.entidades.Add(entidade);

                //HERE IS WHERE I NEED TO SAVE ALL (entidade_endereco, entidade_contato, entidade_banco, entidade)
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(entidade);
        }

But it is only saving the entidade object, the others don't exist anymore when db.SaveChanges() is executed.
How can I insert into the DB with objects that were added to my entity in other scopes?

henrique romao
  • 560
  • 1
  • 8
  • 32

2 Answers2

0

If you really want to make this work as is, you would need to store either the Context (really bad idea) or Entities (slightly less bad) across requests. Session State jumps to mind, but using it can bring in a whole load of new pain.

Ideally, you should change your design to take advantage of the stateless nature of HTTP. Each action method should be a separate transaction, saving the data from it's execution when the method is done. If those separate entities only make sense when they are all saved together, then you need to create all of them within a single action and save them to the context together. Managing the boundaries of different business entities and when they are saved is a critical part of application design, I highly recommend you read about Aggregate Roots within Domain Driven Development. Even if you don't go the full DDD route, the Aggregate Root concept will be extremely helpful to you. The CQRS Journey from Microsoft gives an in-depth tutorial of these concepts (and many others)

Community
  • 1
  • 1
RyanR
  • 7,728
  • 1
  • 25
  • 39
0

Im not sure, if I got your question right (excuse my poor spanish). In the Action Create you only add "entidade" to your entidades collection, and so its the only one affected by SaveChanges(). If you want to add others, include in the Create-Action or try making a EF-transaction.

Without transaction the context is lost after the Create-Method ends