M
M
Michael2016-09-21 14:17:08
Entity Framework
Michael, 2016-09-21 14:17:08

Why is the Context not cleared on Exception?

Good afternoon! Faced the problem of saving the entity in the database, after an error

dbEntities db;
        public RelationshipPeopleRepository(dbEntities context)
        {
            db = context;
        }

        public void Create(relationship_people item)
        {
            db.relationship_people.Add(item);
            db.SaveChanges();
        }

When I call Create, I get the error "Duplicate entry '108-94' for key 'PRIMARY'", I process it with try / catch and want to save information about this to a table in the database.
I'm already doing it in another repository
db.log.Add(item);
db.SaveChanges();

And I still get the error Duplicate entry '108-94' for key 'PRIMARY
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maa-Kut, 2016-09-22
@Slider_vm

Is the context in all repositories the same? If so, then in case of an error in saving the entity, it should be "unhooked" from the context:
In theory, the context after that will stop tracking the entity and will no longer try to shove it into the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question