I am using the Entity Framework 6 and the SqlServer . I have an object called category, where I want to persist it in the database. and when I run this function, it persists the object correctly because I have a datagrid where I can see that it worked. But when I close the application and search again the object is no longer there.
- Why does this occur?
- It is saved only at runtime and does not persists
-
I also noticed that the SaveChanges method returns an integer, has something that indicates if this insertion gave right or wrong? for example, if it returns 1 it works out, if 0 is wrong.
private void btnSalvar_Click(object sender, RoutedEventArgs e) { categoria objCategoria = new categoria(); objCategoria.Id = 0; objCategoria.descricao = ttbDescricao.Text; objCategoria.observacao = ttbObservacao.Text; objCategoria.status = 1; using (SiscabEntities SisEF = new SiscabEntities()) { //SisEF.categoria.Add(objCategoria); if (ttbCodigo.Text.Equals("")) { SisEF.Entry(objCategoria).State = EntityState.Added; } else { SisEF.Entry(objCategoria).State = EntityState.Modified; } SisEF.SaveChanges(); Inicializa(); } }