C# 缺少数据库提供者?
C#。净核心2.2;Visual Studio 2019 我在应用程序中的第一个DB查询中遇到以下异常 尚未为此DbContext配置任何数据库提供程序。可以通过覆盖DbContext.onConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。C# 缺少数据库提供者?,c#,asp.net-core,C#,Asp.net Core,C#。净核心2.2;Visual Studio 2019 我在应用程序中的第一个DB查询中遇到以下异常 尚未为此DbContext配置任何数据库提供程序。可以通过覆盖DbContext.onConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。 如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数 该方法如下所示: public List&
如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数 该方法如下所示:
public List<string> GetList()
{
try
{
using (var db = new CommoditiesContext())
{
var list = (from commodity in db.Commodity
where commodity.DateObsolete.Equals(null)
select commodity.Name.Trim().ToUpper()).OrderBy(a => a).ToList();
return list;
}
}
catch (Exception e)
{
throw new Exception("GetList: " + e.Message);
}
}
CommoditiesContext.cs
文件是自动生成的,其方法如下:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
//warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer("Server=MyDB.company.com;Initial Catalog=THINGS;Integrated Security=True");
}
}
只有在执行上述查询时,才会调用此方法。据我所知,CommoditiesContext
类没有实例化
正如onconfig()
方法中的警告注释所述,在代码中使用连接字符串是个坏主意。酷,我喜欢。我已经注释掉了OnConfiguring()
方法,而是尝试使用:
services.AddDbContext<CommoditiesContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("CommoditiesContext")
)
);
然后使用:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(_config.GetConnectionString("CommoditiesContext"));
}
}
问题是,在调用onconfig()
的过程中,这个自动生成的类似乎没有实例化,因此\u config
为空
我应该怎么做?按照它的设置方式,您需要注入它。 以下是一个例子:
public class MyController : Controller
{
private readonly CommoditiesContext db;
public MyController(CommoditiesContext db)
{
// db is 'automatically' created and given to you.
this.db = db;
}
[HttpGet]
public IActionResult GetList()
{
var list = (from commodity in db.Commodity
where commodity.DateObsolete.Equals(null)
select commodity.Name.Trim().ToUpper()).OrderBy(a => a).ToList();
return this.Ok(list);
}
}
进一步阅读:
补充:
如果您有多个db上下文,可以添加多个,如下所示:
services.AddDbContext<CommoditiesContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("CommoditiesContext")
)
);
services.AddDbContext<AnotherDbContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("AnotherDbContext")
)
);
我缺少指向您的配置/设置文件的链接…谢谢。我已经查看了您共享的文档链接。如果我有多个活动数据库,我会传递整个
IConfiguration
对象来查找所有上下文吗?@7Reeds同一上下文有多个数据库?也许我又错过了一些基本的东西。。。我有多个数据库上下文。在主动控制器中使用了多个控制器。在上面的例子中,我只使用了一个上下文。我假设我能够推断出如何使用多个。我刚刚尝试了I配置
功能。这给了我常规配置,但没有db上下文,据我所知。@7Reeds只注入了多个db上下文。这就是为什么添加db上下文是通用的。我已经猜出了我的答案。
public class MyController : Controller
{
private readonly CommoditiesContext db;
public MyController(CommoditiesContext db)
{
// db is 'automatically' created and given to you.
this.db = db;
}
[HttpGet]
public IActionResult GetList()
{
var list = (from commodity in db.Commodity
where commodity.DateObsolete.Equals(null)
select commodity.Name.Trim().ToUpper()).OrderBy(a => a).ToList();
return this.Ok(list);
}
}
services.AddDbContext<CommoditiesContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("CommoditiesContext")
)
);
services.AddDbContext<AnotherDbContext>(
options => options.UseSqlServer(
Configuration.GetConnectionString("AnotherDbContext")
)
);
public class MyController : Controller
{
private readonly CommoditiesContext comDb;
private readonly AnotherDbContext anoDb
public MyController(
CommoditiesContext comDb,
AnotherDbContext anoDb)
{
this.comDb = comDb;
this.anoDb = anoDb;
}
}