Home  >  Article  >  Backend Development  >  Solution to error when adding EF MVC controller to VS2017

Solution to error when adding EF MVC controller to VS2017

Y2J
Y2JOriginal
2017-04-20 10:00:222318browse

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 DbSet0e70e25b671ebf29d85a9ac685dc29fa 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!

Statement:
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