Entity Framework removing entitdades that has fk

Asked

Viewed 27 times

1

I have in my Repospository, my method of deleting

public virtual void Deletar(int Id)
{
  var entity = _dbSet.Find(Id);
  _dbSet.Remove(entity);
}

The problem is that it is deleting even if it is with FK in another table, and I don’t want it to delete, which is wrong?

  • You have to create database contraints. There is no magic. Or else validate via application, which is also not simple.

  • Fernando, when generating the bank (code first) is already generated with FK

  • Which database you use?

  • Sql Server 2012

  • It was to generate an exception in the database if there is the correct Constraint.

  • Fernando, I found the bug already, was in a configuration of the Entity Framework

Show 1 more comment

1 answer

1


By default, the Entity framework comes with Cascade enabled, so I disabled it and it went on my Exception

  modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

Browser other questions tagged

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