Object is not persisted when using Entity Framework

Asked

Viewed 119 times

-2

I’m using the Entity Framework 6 and the Sqlserver. I have an object called category, where I want to persist it in the bank. and when I run this function, it persists the object correctly because I have a datagrid where I can observe that it worked. But when I close the application and search again the object is no longer there.

  • Why does this happen?
  • It is saved only at runtime and not persists?
  • I also noticed that the method Savechanges returns an integer, has something to indicate whether the insertion was right or wrong? for example, if returns 1 worked, if it is 0 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();
            }
          }
    
  • You have the option to answer your own question, do not put as an edit.

3 answers

1


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

1

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

SisEF.categoria.Add(objCategoria);

When it is changed, you have to locate the record in the table and then make the updates.

categoria catAlterada = new categoria();
catAlterada = SisEF.categoria.find(objCategoria.id);
catAlterada = objcategoria;
SisEf.SaveChanges();
  • 1

    in reality it is attaching the object in the context. the way it works if implemented correctly.

1

The way you are trying to do it is in a disconnected senario, that is, your context has no knowledge of your object that you are trying to persist, so you need to teach the walk 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 its object, the simplest thing to do is to use the add.

Another point you need to change is in editing, you make instance of a category, but do not set the ID value on it when this is editing will give error.

See how you can be doing in this senario.

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 password would be the connected one, in which you would have your object in the context, that is, your object has all the property of the EF and it knows the current state of your 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();
    }
}
  • I just tested your two methods and the same error persists, at first I thought the error could be because the category object is out of using ... but not so solve. I have two screens one for registration and one for search, while the program is running I can register and view the objects on the search screen normally, but when I close and open the application again they disappear. entered some items manually into the database, and in the search screen they appear normally.

  • I just did a debug and when it arrives in Savechanges the state is close

  • This could not be happening. You must have some Dysplasia implemented somewhere. Your context should be in one of the two states. Add or modified

  • If you can help me with this question: https://answall.com/questions/264673/salvando-um-related-muitos-para-muitos-entity-framework

Browser other questions tagged

You are not signed in. Login or sign up in order to post.