为EF,AddDbContext或AddEntityFramework添加服务

Tho*_*ian 5 entity-framework-core asp.net-core

有没有之间的差异services.AddDbContextservices.AddEntityFramework().AddDbContext?他们的展位看起来在core-1.0.0下有效。

选择A

services.AddDbContext<DbContext>(options => options.UseSqlServer(connection));
Run Code Online (Sandbox Code Playgroud)

选择B

services.AddEntityFramework()
    .AddDbContext<DbContext>(options =>
        options.UseSqlServer(connection));
Run Code Online (Sandbox Code Playgroud)

还注意到.AddSqlServer()看起来不见了。上次我在RC2上进行过一些核心工作时,我发现很多东西已经被清理/重命名/简化了,我想这很好。

Isa*_*aac 2

我引用的是:当您希望实体框架解析它从外部 IServiceProvider 使用的服务时,您只需要使用此功能(AddEntityFramework)。如果您不使用外部 IServiceProvider,实体框架将负责创建它所需的服务。

请参阅: https: //github.com/aspnet/EntityFramework/blob/dev/src/Microsoft.EntityFrameworkCore/Infrastruct/EntityFrameworkServiceCollectionExtensions.cs

我引用的是:故意在这个命名空间中,因为这是供其他关系提供者使用的,而不是供顶级应用程序开发人员使用的。

请参阅: https: //github.com/aspnet/EntityFramework/blob/94138e66a56693395e5e323a8d4b666bbcb07bf7/src/Microsoft.EntityFrameworkCore/Infrastruct/EntityFrameworkServiceCollectionExtensions.cs