Saving a Many-to-Many Relationship with Entity Framework

1

Questions when inserting objects into the database when using Entity Framework 6 + C # WPF.

I have 3 objects, ServiceOrder, Service, and ItemsOs. with the following structure:

public partial class OrdemServico
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public OrdemServico()
    {
        this.ItensOs = new HashSet<ItensOs>();
    }

    public int Id { get; set; }
    public Nullable<System.DateTime> Data { get; set; }
    public decimal Total { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ItensOs> ItensOs { get; set; }
    }
}



public partial class Servico
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Servico()
    {
        this.ItensOs = new HashSet<ItensOs>();
    }

    public int Id { get; set; }
    public string Descricao { get; set; }
    public string Observacao { get; set; }
    public decimal ValorVenda { get; set; }
    public int CategoriaID { get; set; }

    public virtual Categoria Categoria { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ItensOs> ItensOs { get; set; }

    }
}


public partial class ItensOs
{
    public int OrdemServicoID { get; set; }
    public int ServicoID { get; set; }
    public int Quantidade { get; set; }
    public decimal ValorUnitario { get; set; }
    public decimal Total { get; set; }

    public virtual OrdemServico OrdemServico { get; set; }
    public virtual Servico Servico { get; set; }
}

In my View I have a global variable of type List ItemsOs, where I store the services that have been performed.

ItensOs o = new ItensOs();

o.OrdemServicoID = SeguraServico.Id;
o.Quantidade = Quantidade;
o.ValorUnitario = SeguraServico.ValorVenda;
o.Total = Quantidade * SeguraServico.ValorVenda;

ListaDeServico.Add(o); // Essa é minha váriavel global.

So when I'm going to save this in the database I do this:

 using (SiscabEntities SisEF = new SiscabEntities())
 {
      OrdemServico o = new OrdemServico();

      o.Descricao = ttbDescricao.Text;
      o.Data = Convert.ToDateTime(ttbData.Text);
      o.Observacao = "";
      o.Total = AtualizaTotal();
      **//o.ItensOs = ListaDeServico;  <~~ Explicação no final** 

      if (ttbCodigo.Text.Equals("")) 
      { 
           SisEF.OrdemServico.Add(o);
      // Quando você salva o objeto, o proprio savechanges já retorna o Id para o proprio Objeto.
           SisEF.SaveChanges();

           foreach(ItensOs oItens in ListaDeServico)
           {
              oItens.OrdemServicoID = o.Id;
              SisEF.ItensOs.Add(oItens);
           }

      }

      SisEF.SaveChanges();
}

This way I first saved my work order and after I saved the items in the work order. however the following error occurs:

  

System.Data.Entity.Infrastructure.DbUpdateException: 'Unable to update   the EntitySet 'Items' because it has a DefiningQuery and no    element exists in the   element to support the current operation. '

My question is, why does this error occur? apparently I'm not using the best programming method but the algorithm should work.

Is it possible to save my ServiceList object in the ServiceOrder, and when I save it it saves all at once? dispensing the foreach? // o.ItensOs = ServiceList; < ~~ Explanation at the end

    
asked by anonymous 22.12.2017 / 00:20

0 answers