Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear if it's possible to register in the DbContext's constructor or not #13

Open
cbp123 opened this issue Aug 14, 2020 · 1 comment

Comments

@cbp123
Copy link

cbp123 commented Aug 14, 2020

Hi there,
We are using this package in EF6 and it's working well, but running into trouble with where to register hooks exactly.

The logical place is in the constructor of the DbContext, but that can run into trouble as the very first time you construct the DbContext, the model may not have been created yet. This is a problem for unit tests.

Registering in OnModelCreating doesn't work either though.

public class MyDbContext : DbContext { public MyDbContext() { this.OnSave().Attach(new MyHook()); // Fails the first time the context is constructed, as model has not been created yet. } }

Wondering what strategy you take for registering the hooks.
Cheers

@dombrovsky
Copy link
Owner

Hi,
I've been using it from constructor, just like you do, but in code-first scenario.
You may try though registering hooks outside of MyDbContext class, e.g.

var dbContext = new MyDbContext();
dbContext.OnSave().Attach(new MyHook());

Could you provide more details on your unit tests scenario, or a sample project where I can check it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants