如何将我的 AddDbContext<ContainerContext> 注入我的 DAL 项目(Core 3.1)



我使用的是MVC 5,Core 3.1

我已将"AddDbContext"添加到Startup.cs.中的服务中

然后我有一个类库核心3.1项目,它是我的ADODal层。这也是Startup.cs的ConfigureServices中添加的一项服务。

我想将连接字符串注入DAL应用程序。

我有:

public partial class ContainerContext : DbContext
{
public ContainerContext()
{
}
public ContainerContext(DbContextOptions<ContainerContext> options) : base(options)
{
}
}

在Startup.cs 中

public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ContainerContext>(options => options.UseSqlServer(connection));
services.AddDAL();
}

在Dal项目中:

public static class ServiceCollectionExtensions
{
// Add parameters if required, e.g. for configuration
public static IServiceCollection AddDAL(this IServiceCollection services)
{
// Register all services as required
services.AddScoped<ILeaseBll, LeaseDal>();
return services;
}
}

达尔班。

public class LeaseDal :  ILeaseBll 
{
private string conString;
public LeaseDal(???????)
{
// Some validation for the Context maybe (isNull etc?) throw new ArgumentNullException("conString");
//this.connectionString = conString;
}

应该怎样做?感谢

Dot-Net Core和Dot-Net Framework的理念发生了变化。。。。

public class LeaseDal :  ILeaseBll 
{
private string conString;

这不是点网核心的最佳实践。

你不注射你的";连接字符串";在具体的DataAccessLayer对象中。

注入数据库上下文。

(数据库上下文已经连接到Ioc…并带有正确的连接字符串(

类似这样的东西:

public interface IDepartmentQueryDomainData()
{
Task<int> GetCountAsync(CancellationToken token);
}

public class DepartmentQueryEntityFrameworkDomainDataLayer : IDepartmentQueryDomainData
{
public const string ErrorMessageILoggerFactoryIsNull = "ILoggerFactory is null";
public const string ErrorMessageMyCoolDbContextIsNull =
"MyCoolDbContext is null";

private readonly ILogger<DepartmentQueryEntityFrameworkDomainDataLayer> logger;
private readonly MyCoolDbContext entityDbContext;

public DepartmentQueryEntityFrameworkDomainDataLayer(
ILoggerFactory loggerFactory,
MyCoolDbContext context
{
if (null == loggerFactory)
{
throw new ArgumentNullException(ErrorMessageILoggerFactoryIsNull, (Exception)null);
}
this.logger = loggerFactory.CreateLogger<DepartmentQueryEntityFrameworkDomainDataLayer>();

this.entityDbContext = context ?? throw new ArgumentNullException(
ErrorMessageMyCoolDbContextIsNull,
(Exception)null);
}
public async Task<int> GetCountAsync(CancellationToken token)
{
int returnValue = await this.entityDbContext.Departments.AsNoTracking().CountAsync(token);
this.logger.Log(
new LogEntry(
LoggingEventTypeEnum.Trace,
string.Format(
LogMessages.Count,
returnValue)));
return returnValue;
}
}

你也可以"参见";这里是:

https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

public class MyController
{
private readonly ApplicationDbContext _context;
public MyController(ApplicationDbContext context)
{
_context = context;
}
}

我永远不会将dbContext注入到";控制器";。。。(我同意你的看法,达尔应该是一个单独的层(。。。但除此之外;miscue";在microsoft的例子中,您确实看到您注入了dbContext。

另请参阅:

https://hovermind.com/aspnet-core/using-dbcontext-with-dependency-injection.html

最新更新