Home > Backend Development > C#.Net Tutorial > Solution to error when adding EF MVC controller to VS2017

Solution to error when adding EF MVC controller to VS2017

Y2J
Release: 2017-04-20 10:00:22
Original
2439 people have browsed it

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);
}
Copy after login

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();
}
Copy after login

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 This complete statement can solve

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!

Related labels:
mvc
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template