Error Performing Update on Entity Framework

Asked

Viewed 69 times

1

I have the following relationship between Agenda and Contact:

  • An Agenda may have N Contacts;

  • A contact can be in N Agendas;

In other words, I have an N:M relationship between the Agenda and Contact entities;

When creating an Agenda with the contact list the E.F Add action goes perfectly, but when trying to include a new Contact in an already created Agenda it happens that the SaveChangesreturns 1 but in the base there is no change, that is, in the table Schedulecontact is not adding the IDconcerning the Agenda and the new contact.

Code that adds the new Contact:

public BookModel AddNewContact(ContactModel newcontact, Guid bookId)
{
      Contact contact = null;

      var contatoexiste = _contactRepository.Exist(newcontact.contactId);

      if (contatoexiste)
            contact = _contactRepository.Search(x => x.contactId == 
          newcontact.contactId).FirstOrDefault();

      else
      {
          var newcontactdomain = Mapper.Map<Contact>(newcontact);

         contact = _contactRepository.AddOrUpdate(newcontactdomain);
       }

       try
       {
            var book = _bookRepository.GetById(bookId);

            book.Customers.Add(contact);

            _bookRepository.Update(book);

            return Mapper.Map<BookModel>(book);

        }
        catch (Exception ex)
        {

            throw;
        }

  }

Update Code (Repository):

public T Update(T entity)
{
     _Dbset.Attach(entity);

      Context.Entry(entity).State = EntityState.Modified;

      var result = Context.SaveChanges(); //retorna 1 se ok 

      return entity;
}
  • _Bookrepository.Getbyidnotracking(guid) is actually using Asnotracking?

  • edited the question and added the code from the repository that makes the AsNoTracking

  • https://answall.com/questions/388416/porque-%C3%A9-que-o-Ef-core-est%C3%a1-a-actualization-data-of other tables

No answers

Browser other questions tagged

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