Skip to content

Add a lightweight version of DbContext #19610

New issue

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

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

Already on GitHub? # to your account

Closed
AndriySvyryd opened this issue Jan 16, 2020 · 9 comments
Closed

Add a lightweight version of DbContext #19610

AndriySvyryd opened this issue Jan 16, 2020 · 9 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jan 16, 2020

One without OnConfiguring and OnModelCreating methods. These can be optionally specified in AddDbContext.

Optimized for use with compiled model and context pooling. It will allow using EF without loading any model building types.
OnModelBuilding can be moved to options: DbContextOptionsBuilder.OnModelBuilding(Action<ModelBuilder> buildModel)

Consider doing something similar for DatabaseFacade if migration code is not trimmed out even when EnsureCreated/EnsureDeleted is not used.

Limitations:

  • It won't have events or other features that need to be cleaned when returning to pool

Alternatively, we can define a feature switch for model building to allow the code to be trimmed.

@roji
Copy link
Member

roji commented Jan 16, 2020

Would this be a user-facing thing, or more of an internal building block for compiled models?

@AndriySvyryd
Copy link
Member Author

AndriySvyryd commented Jan 16, 2020

This is user-facing, they will need to place the model building code in a different assembly (along with migrations) than this runtime context to be able to avoid loading model building code in production.

@roji
Copy link
Member

roji commented Jan 16, 2020

OK, thanks for the explanation. It would be nice to do a design session once where you give an overview of what you're planning :)

@AndriySvyryd
Copy link
Member Author

Sure, but I don't think that the benefits of doing this will ever be enough to justify the cost of implementation.

@ajcvickers
Copy link
Contributor

@AndriySvyryd Do you mean you created an issue that you think we will close? :trollface:

The question of how to create the compiled model and consume it at runtime has been bothering me for five years! This does seem like it could be a way forward.

@AndriySvyryd
Copy link
Member Author

Yes, as a pragmatic optimist while I don't think we'll do this I hope we will.

@ilmax
Copy link
Contributor

ilmax commented Jan 17, 2020

@ajcvickers I'm sure you guys already thought about it, but what about starting with a serializable implementation of the IModel and generate that one via a command line at CI time, and configure the context to load this serialized model in the startup.cs.
I was thinking about generating a read-only model at compile time but without running ef this is tricky, this approach instead looks a bit easier to me.

@AndriySvyryd
Copy link
Member Author

@ilmax Yes, we've thought about it 😄 : #1906 (comment), #8258, #12511

@AndriySvyryd
Copy link
Member Author

This is no longer necessary due to how trimming works.

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

No branches or pull requests

4 participants