dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

Add a lightweight version of DbContext #19610

Closed AndriySvyryd closed 1 year ago

AndriySvyryd commented 4 years ago

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:

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

roji commented 4 years ago

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

AndriySvyryd commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ajcvickers commented 4 years ago

@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 commented 4 years ago

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

ilmax commented 4 years ago

@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 commented 4 years ago

@ilmax Yes, we've thought about it 😄 : https://github.com/dotnet/efcore/issues/1906#issuecomment-401565817, https://github.com/dotnet/efcore/issues/8258, https://github.com/dotnet/efcore/issues/12511

AndriySvyryd commented 1 year ago

This is no longer necessary due to how trimming works.