How can I solve the error when I add an EF MVC controller in VS2017?

Source: Internet
Author: User

How can I solve the error when I add an EF MVC controller in VS2017?

For your reference, the following describes how to report errors to the MVC controller added to EF in VS2017:

1. Error description:No database provider has been configured fot this DbContext.

This type of error is caused by context registration. Solution: rewrite the oninserting method in DBContext to inject database connections.

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; // pass the configuration connection to services in DbContext. addDbContext <DbContext> (options => options. useSqlServer (sqlserverConnection); services. addMvc ();}

2. Error description:Cocould not add Model type XXX to DbContext

The error description does not contain the DbSet attribute. but it is actually registered with public DbSet <XXX> XXX {get; set. change the class in DbSet <XXX> to the complete declaration of <namespace + Class Name>.

For more information, click Visual Studio 2017 development and usage tutorial. for visual studio installation tutorial, click Visual Studio installation user manual.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.