Adding service for EF, AddDbContext or AddEntityFramework

asp.net-core entity-framework-core

Question

Is there a difference between services.AddDbContext and services.AddEntityFramework().AddDbContext? They booth looks to be valid under core-1.0.0.

Choice A

services.AddDbContext<DbContext>(options => options.UseSqlServer(connection));

Choice B

services.AddEntityFramework()
    .AddDbContext<DbContext>(options =>
        options.UseSqlServer(connection));

Also noticed that .AddSqlServer() looks to be gone. Last time I did some work on core was RC2 and I see that a lot has been cleaned/renamed/simplified, I guess that's nice.

1
6
7/22/2016 9:41:26 AM

Accepted Answer

I'm quoting: You only need to use this functionality (AddEntityFramework) when you want Entity Framework to resolve the services it uses from an external IServiceProvider. If you are not using an external IServiceProvider, Entity Framework will take care of creating the services it requires.

See: https://github.com/aspnet/EntityFramework/blob/dev/src/Microsoft.EntityFrameworkCore/Infrastructure/EntityFrameworkServiceCollectionExtensions.cs

I'm quoting: Intentionally in this namespace since this is for use by other relational providers rather than by top-level app developers.

See: https://github.com/aspnet/EntityFramework/blob/94138e66a56693395e5e323a8d4b666bbcb07bf7/src/Microsoft.EntityFrameworkCore/Infrastructure/EntityFrameworkServiceCollectionExtensions.cs

2
7/22/2016 4:28:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow