Asp.net core 如何使用Entityfrmework从Asp.netcore访问数据库
在这里,我设置了从Anguar2到数据库的一些Db连接,但当我尝试点击数据库时,我得到的错误是Asp.net core 如何使用Entityfrmework从Asp.netcore访问数据库,asp.net-core,Asp.net Core,在这里,我设置了从Anguar2到数据库的一些Db连接,但当我尝试点击数据库时,我得到的错误是附加信息:没有为此DbContext配置数据库提供程序。可以通过覆盖DbContext.onconfig方法或使用 这是我的数据库连接 { "connectionStrings": { "DefaultConnection": "Server=MD;Database=Md;userid=sa;password=123;Trusted_Connection=True;MultipleActiv
附加信息:没有为此DbContext配置数据库提供程序。可以通过覆盖DbContext.onconfig方法或使用
这是我的数据库连接
{
"connectionStrings": {
"DefaultConnection": "Server=MD;Database=Md;userid=sa;password=123;Trusted_Connection=True;MultipleActiveResultSets=true;"
}
startUp.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<StudentContext>(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
public class StudentContext:DbContext
{
public StudentContext(DbContextOptions<StudentContext> options) : base(options) { }
public StudentContext() { }
public DbSet<StudentMaster> StudentMaster { get; set; }
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(option=>option.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.AddMvc();
}
DbContext.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<StudentContext>(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
public class StudentContext:DbContext
{
public StudentContext(DbContextOptions<StudentContext> options) : base(options) { }
public StudentContext() { }
public DbSet<StudentMaster> StudentMaster { get; set; }
}
公共类StudentContext:DbContext
{
public StudentContext(DbContextOptions选项):基本(选项){}
public StudentContext(){}
公共DbSet StudentMaster{get;set;}
}
在appsettings.json中引用正确的变量(或文件名)
然后确保为您的案例获取(ConnectionString:DefaultConnection)
}appsetting.json我在写这个文件时提到过,你能告诉我这是r8还是配置错误吗
public class StudentContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer("_connectionString_", _options => _options.EnableRetryOnFailure());
}