This article mainly introduces in detail the solution to the error of adding EF MVC controller to VS2017. It has certain reference value. Interested friends can refer to
VS2017 adding EF MVC The solution to the error reported by the controller is for your reference. The specific content is as follows
1. Error description:no database provider has been configured fot this DbContext.
This type of error is caused by context registration. The solution is to override the OnConfiguring method in DBContext to inject the database connection.
In DbContext:
public static string ConnectionString { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(ConnectionString); base.OnConfiguring(optionsBuilder); }
In Startup.cs
public void ConfigureServices(IServiceCollection services) { // Add framework services. var sqlserverConnection = Configuration.GetConnectionString("SQLServerConnection"); DbContext.ConnectionString = sqlserverConnection;//将配置连接传入DbContext中 services.AddDbContext<DbContext>(options => options.UseSqlServer(sqlserverConnection)); services.AddMvc(); }
2. Error description: Could not add Model type XXX to DbContext
The error description is that the DbSet attribute is not registered. But in fact, it is registered with public DbSet
The above is the detailed content of Solution to error when adding EF MVC controller to VS2017. For more information, please follow other related articles on the PHP Chinese website!