1

When trying to scaffold a new controller with views in my project, I get the following error: (Yes, the image shows DEBUG connection... I changed them all to DEFAULT after I took the screenshot of the error)

(In case the image doesn't show) There was an error running the selected code generator: 'A named connection string was used, but the name DefaultConnection was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. see https://go.microsoft.com/fwlink/?linkid=850912 for more information.

However, I am using IConfiguration and both Startup.cs and database context file reference that connection.

When scaffolding the dbcontext, I can use DefaultConnection and it pulls in all the changes from the SQL database I'd expect to see in my project - just then refuses to scaffold a controller.

Startup.cs

        public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddDbContext<CJFresh>(options =>
                   options.UseSqlServer(
                       Configuration.GetConnectionString("DefaultConnection")));

Database Context File:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Name=DefaultConnection");
        }
    }

AppSettings.json

    {
  "AllowedHosts": "*",
  "ConnectionStrings": {

    "DefaultConnection": "Server=SERVER;Database=DATABASE;Trusted_Connection=False;user id=USERX;password=PASSWORDX;MultipleActiveResultSets=true",
   
  }
 }

I've tried some of the solutions I've seen in other posts, but they don't seem to work for me. For example, I've tried all the edits in this post: .Net Core 2 EF core connection string problem

I'm sure it's something simple I'm missing, but I just can't see it right now.

2
  • Do a solution-wide search for "DebugConnection" and see what pops up. Sep 11, 2020 at 14:15
  • Sorry, should update my post.. I'd switched everything to back to Default Connection after I'd taken the screenshot. Sep 11, 2020 at 14:22

1 Answer 1

1

I solved this problem by copying the entire SQL Server connection string from appsettings.json and pasting it in place of the below code, within the UseSqlServer method. For an unknown reason the automatic Razor View build wizard in Visual Studio won't read from the json connection string variable. The error for me happened after I did a scaffold-dbcontext which created models and a db context class from database tables.

optionsBuilder.UseSqlServer("name=ACConnection");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.