获取EF Core DbContext的连接字符串



我正试图为EF Core编写一个dbcontext,我需要从我的IConfiguration中获得一个连接字符串。如果我在该类的构造函数中请求配置,它应该通过依赖注入获得它,如果我使用这个?

services.AddDbContext<PostgresContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("BloggingContext")));

上面的代码是从Npgsql EFCore指令,但我不知道配置是从哪里来的。我真的需要你的帮助。我应该在这里使用AddSingleton方法,让DI完成它的工作吗?

这是我的课程,如果你感兴趣的话

namespace Foo
{
public class PostgresContext : DbContext
{
private PostgresSettings settings { get; init; }
PostgresContext(IConfiguration configuration) 
{
settings = configuration.GetSection(nameof(PostgresSettings)).Get<PostgresSettings>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(settings.getConnectionString());
}
}
}

你的DbContext(子类)不应该被命名为PostgresContext或包含任何postgres特定的代码。

你的dbContext应该命名为

"MyAppDbContext".

这将包含您的表单实体及其映射的DbSet(s)。

同样,它不应该包含postgres特定的东西。

现在,你需要在某个地方说"我想要用于我的EF设置的具体是postgresql"。

那应该是一个地方。你的国际奥委会注册。DotNet-Core的"IoC容器"被称为IServiceCollection。

我的postgres IoC注册是这样的:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
string completeConnectionString = /* get this from your IConfiguration */;

services.AddDbContext<MyAppDbContext>(
optionsBuilder => optionsBuilder.UseNpgsql(
completeConnectionString,
options => options.EnableRetryOnFailure(
maxRetryCount: 3,
maxRetryDelay: TimeSpan.FromMilliseconds(100),
errorCodesToAdd: null))
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking),
ServiceLifetime.Transient);
(旁注,你不应该盲目地使用TrackingBehavior和ServiceLifetime项。我的用法是Asp。Net-Core应用程序。如果你有一个"winforms"应用程序类型(client-install..)你可以有不同的设置)

当你正在使用postgres时,你应该能够通过更改AddDbContext行来轻松地更改为Ms-Sql-Server。这是一个"泄密故事"。表明你确实把事情做得"很好"。

你"names"你的对象不应该绑定到一个特定的RDBMS。

EntityFramework-Core(或任何ORM)是对特定RDBMS的抽象。

这是一个保持你的代码"干净"的建议。

你可能/应该-可能有一个关于实体-框架- orm关注的。csproj。

它应该引用(版本可能是3.1'或6.0'或7.0')

但是要注意包装。

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.10" />

注意,没有任何引用是"postgresql";具体。

.

then in your "top-entry-point"层(以asp.net core应用程序或。net-core控制台应用程序为例)…你有一个参考:

您将拥有当前目标的postgres包(或ms-sql-server或任何rdbms-to-entity-framework-core包)。

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />

最后一个包引用允许您使用"UseNpgsql"来执行AddDbContext。"choice".

简而言之,您不是专门为postgres编写EF-Core代码。(EF-Core不是ADO。. NET,在ADO。你可能会写一些rdbms特有的代码…除非你只使用最基本的SQL语句)。

您正在编写EF-Core代码(再次,从特定RDBMS的抽象)..........和(仅)AddDbContext/UseNpgsql你选择一个具体的(rdm -provider)使用。

这里有一个链接作为例子。

https://github.com/granadacoder/dotnetcore-hostedservice-containerized-one/blob/master/src/DataLayer.EntityFramework/Contexts/WorkerServiceExampleOneDbContext.cs L29L39

它显示了DbContext中会发生的事情。它不应该是任何postgres(或任何rdbms)特定的。

你也可以"上网搜索";以下2项:

AddDbContext UseNpgsql

,你会发现这样的文章:

https://code-maze.com/configure-postgresql-ef-core/

你会注意到它们的"ApplicationContext"有DbSet(s)在里面…

最新更新