williamsandonz
williamsandonz

Reputation: 16430

How to pass active DB Context to Entity framework validator

I have a validator which needs to check the database. For example, it needs to check if a user with the same email address or something like that.

My question is, how to do you pass the active DB Context into the validator to save having to create a new one?

Or am I missing the point?

Is an extra open DB context inconsequential?

Upvotes: 2

Views: 569

Answers (1)

Tormod Haugene
Tormod Haugene

Reputation: 3686

This is a very good question! One that I have been wondering about myself. I found this answer to be very good:

One DbContext per web request... why?

Is an extra open DB context inconsequential?

If you look at any auto-generated controller code for MVC applications, you will notice how each controller always instantiates another DBContext object. I personally understand this such that having several active contexts in principle is not an issue. Personally I rather create another DBContext instance than complicating the code by by sending them to Methods, and having to keep track of when to save changes, and what gets affected by the savechange action (talked about in the link). On the other hand, I would not instantiate new DBContexts for every iteration of a loop, or any other cases where the rather small overhead would stack.

Hope this helps.

Upvotes: 2

Related Questions