Object is not persisted when using Entity Framework

-2

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();
            }
          }
    
asked by anonymous 16.12.2017 / 02:12

3 answers

1

The project's local database is in the same project folder, whenever I run the project it throws the database into the bin / debug creating a new database. what should be done to solve the problem is to go to the properties of the .mdf database file and change the 'Change to output directories' field and put the 'Copy if newer' option

    
27.12.2017 / 21:13
1

Good afternoon. For the posted code, the objCategory object is missing from the category table. To include:

SisEF.categoria.Add(objCategoria);

When you change, you have to find the record in the table and then make the updates.

categoria catAlterada = new categoria();
catAlterada = SisEF.categoria.find(objCategoria.id);
catAlterada = objcategoria;
SisEf.SaveChanges();
    
19.12.2017 / 19:51
1

The form you are trying to do is in a disconnected sen- sor, meaning your context is not aware of your object you are trying to persist, so you need to teach the path of the stones to it. The first point you need to change is in your EntityState.Added; this works, but you need to use Attach with your object, the simplest thing to do is to use the add.

Another point that you need to change is in the edit, you make an instance of a category, but not setting the value of the ID in it when it is editing will give an error.

See how you can be doing in that session.

private void btnSalvar_Click(object sender, RoutedEventArgs e)
{
    using (SiscabEntities SisEF = new SiscabEntities())
    {
        categoria objCategoria = new categoria();
        objCategoria.descricao = ttbDescricao.Text;
        objCategoria.observacao = ttbObservacao.Text;
        objCategoria.status = 1;

        if (ttbCodigo.Text.Equals(""))
        {
            // veja o nome correto  da categoria no seu contexto
            SisEF.Categorias.Add(objCategoria);
        }
        else
        {
            objCategoria.Id = (int)ttbCodigo.Text;
            SisEF.Attach(objCategoria);         
            SisEF.Entry(objCategoria).State = EntityState.Modified;
        }
        SisEF.SaveChanges();
        Inicializa();
    }
}

Another senario would be the connected, in it you would have its object in context, ie its object has all the properties of the EF and it knows the current state of its object.

private void btnSalvar_Click(object sender, RoutedEventArgs e)
{
    using (SiscabEntities SisEF = new SiscabEntities())
    {
        if (!ttbCodigo.Text.Equals(""))
        {           
            var categoria = SisEF.Categorias.Find(1ttbCodigo.Text); // todo convert to int ...
            categoria.descricao = ttbDescricao.Text;
            categoria.observacao = ttbObservacao.Text;
            categoria.status = 1;   
        }   
        else
        {
            categoria objCategoria = new categoria();
            objCategoria.descricao = ttbDescricao.Text;
            objCategoria.observacao = ttbObservacao.Text;
            objCategoria.status = 1;
            SisEF.Categorias.Add(objCategoria);
        }

        SisEF.SaveChanges();
        Inicializa();
    }
}
    
19.12.2017 / 20:28