1
Doubts when inserting objects into the database when using Entity Framework 6 + C# WPF.
I have 3 objects Ordemservico, Service and the Itensos. 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 Itensos, where I store the services that were 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 to the database I do it this way:
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 save first my service order and after I save the service order items. but the following error occurs:
System.Data.Entity.Infrastructure.Dbupdateexception: 'Unable to update the Entityset 'Itensos' because it has a Definingquery and no element exists in the element to support the Current Operation.'
My question is, why is this error occurring? apparently I’m not using the best programming method but the algorithm should work.
it is possible to save my Deservic List object in Orderdeservico, and when I save it saves everything at once? dispensing the foreach? //o. Items = Deserving list; <~~ Explanation at the end
If you have been added your Itensos will need to set the Ordemservicoid there is no way you will have to make the loop even, unless this value already comes in your list. ai you could use Sisef.ItensOs.Addrange(oIItens); but I believe your error is because EF is adding an object you already have in the database, or your PK ID property is not auto increment and is causing this error.
– Marco Souza
I believe this answer helps ... https://stackoverflow.com/a/7583791/2740371
– Marco Souza