C# Asp.net core和Entityframwork迁移错误
我有models文件夹,文件夹中有Todo.cs和TodoContext.cs 在Todo.cs中,我的代码是:C# Asp.net core和Entityframwork迁移错误,c#,asp.net,.net,asp.net-core,entity-framework-core,C#,Asp.net,.net,Asp.net Core,Entity Framework Core,我有models文件夹,文件夹中有Todo.cs和TodoContext.cs 在Todo.cs中,我的代码是: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ApiCrudWithEfCore.Models { public class Todo { public int Id { get; s
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ApiCrudWithEfCore.Models
{
public class Todo
{
public int Id { get; set; }
public string title { get; set; }
public bool Iscomplete { get; set; }
}
}
在TodoContext中,我有:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace ApiCrudWithEfCore.Models
{
public class TodoContext : DbContext
{
public TodoContext(DbContextOptions<TodoContext> options) :base(options) {}
public DbSet<Todo> Todos { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
使用System.ComponentModel.DataAnnotations;
命名空间ApiCrudWithEfCore.Models
{
公共类TodoContext:DbContext
{
公共TodoContext(DbContextOptions选项):基本(选项){}
公共DbSet Todos{get;set;}
}
}
之后,我将连接字符串放入appsettings.json中:
{
连接:“服务器=(localdb)\mssqllocaldb;数据库=Todo;受信任的连接=True;”,
“日志记录”:{
“日志级别”:{
“默认值”:“警告”
}
},
“AllowedHosts”:“*”
}
我在startup.cs中使用它,如下所示:
public void ConfigureServices(IServiceCollection services,IConfiguration config)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
}
{
"ConnectionStrings": { "todo": "Server=(localdb)\\mssqllocaldb;Database=Todo;Trusted_Connection=True;" },
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
public IConfiguration _config { get; set; }
public Startup(IConfiguration configuration) {
_config = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(_config.GetSection("ConnectionStrings")["todo"]));
}
public void配置服务(IServiceCollection服务,IConfiguration配置)
{
services.AddMvc();
services.AddDbContext(options=>options.UseSqlServer(config.GetConnectionString(“connection”));
}
但当我使用Add Migration命令时,会出现以下错误:
访问类“Program”上的IWebHost时出错。
在没有应用程序服务提供商的情况下继续。错误:错误
ConfigureServices方法必须是无参数的,或者只能使用一个参数
IServiceCollection类型的参数。无法创建的对象
键入“TodoContext”。针对设计时支持的不同模式
时间,看
我读过,但找不到对我有帮助的信息。有人能帮我吗?当使用
GetConnectionString
时,你的appsettings.json
应该包含一个名为ConnectionString的部分,里面你需要有一个连接名(例如“todo”)作为键,并计算实际连接字符串的值
例如:
"ConnectionStrings": {
"todo": "Server=(localdb)\mssqllocaldb;Database=Todo;Trusted_Connection=True;"
},
-- Rest of your appsettings
然后您将像这样使用它:config.GetConnectionString(“todo”)
引用自,GetConnectionString是一个:
GetSection(“ConnectionString”)[name]的缩写
请将您的配置更改为:
"ConnectionStrings": {
"connection": "Server=Yourserver;Database=DAWIDARI;Trusted_Connection=true;"
},
并在Startup.cs中调用它
services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
services.AddDbContext(options=>options.UseSqlServer(config.GetConnectionString(“connection”));
我首先发现了我的问题,我按照建议更改了appsettings.json文件,它看起来像这样:
public void ConfigureServices(IServiceCollection services,IConfiguration config)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
}
{
"ConnectionStrings": { "todo": "Server=(localdb)\\mssqllocaldb;Database=Todo;Trusted_Connection=True;" },
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
public IConfiguration _config { get; set; }
public Startup(IConfiguration configuration) {
_config = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(_config.GetSection("ConnectionStrings")["todo"]));
}
但它没有解决问题,因此我开始检查startup.cs并更改注入appsettings的方式,使startup.cs文件如下所示:
public void ConfigureServices(IServiceCollection services,IConfiguration config)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(config.GetConnectionString("connection")));
}
{
"ConnectionStrings": { "todo": "Server=(localdb)\\mssqllocaldb;Database=Todo;Trusted_Connection=True;" },
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
public IConfiguration _config { get; set; }
public Startup(IConfiguration configuration) {
_config = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<TodoContext>(options => options.UseSqlServer(_config.GetSection("ConnectionStrings")["todo"]));
}
public-IConfiguration\u-config{get;set;}
公共启动(IConfiguration配置){
_配置=配置;
}
//此方法由运行时调用。使用此方法向容器中添加服务。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
services.AddDbContext(options=>options.UseSqlServer(_config.GetSection(“connectionString”)[“todo]”));
}
我在启动构造函数中插入Iconfiguration,定义_configvar并为其分配配置,然后我使用GetSection获取连接字符串您也可以用这种方式尝试注册数据库上下文:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TodoContext>(options => options.UseSqlServer(Configuration.GetConnectionString("connection")));
...
}
公共IConfiguration配置{get;}
公共启动(IConfiguration配置)
{
配置=配置;
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“connection”));
...
}
或者,您可以手动注册数据库上下文:
services.AddTransient<TodoContext>();
services.AddTransient();
这是您的完整appsettings.json吗?@Haytam是的,isI更改了它,但我认为我的主要问题在于配置我认为我以错误的方式注入了它我更改了它,但我认为我的主要问题在于配置我认为我以错误的方式注入了它您是否尝试了我获取连接字符串的方式?