locked
ASP.NET 5 / MVC 6 Database First - No connection string named MyEntity could be found in the application config file RRS feed

  • Question

  • User-1882553206 posted

    Our ASP.NET 5.0 / MVC 6 project accesses our DB by going through a Business layer (assembly) then down into DAL (Database First) assembly.  The MVC 6 code blows up when trying to execute the Business layer code which uses the DbContext to access the DB with error: No connection string named 'MyEntity' could be found in the application config file.  I have tried defining the MyEntity connection string in the MVC project config.json, appsettings.json with no luck.

    If executing Business layer methods from a tester project where I have the expected app.config file with connection string syntax as below it works no problem.  Note: MVC versions prior to latest ASP.NET 5.0 / MVC 6 simply needed the connection string added to the web.config file to work as needed, but now unsure how to get the needed application configuration value defined in new MVC 6 framework - so it works with Database First DAL Context defined object?

    Any help would be appreciated, let me know if you need any more information.

    <add name="MyEntity" 

    connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyServer;initial catalog=MyDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
    providerName="System.Data.EntityClient" />

    Friday, February 12, 2016 12:43 AM

Answers

  • User-1882553206 posted

    I was incorrectly relying on EF to pick up the connection string automatically. This worked fine for MVC 5 (and previous versions) running with Database First model where we could specify the expected DB connection name, but with MVC 6 will need to pass in the connection string explicitly.

    I added the additional ctor taking the connection string in its own partial class file, since we're doing Database First and the context file gets overwritten with successive model updates from DB. I.e having in separate partial class / file preserves new ctor to not be overwritten.

    public partial class MyDbContext : DbContext
    {
    public MyDbContext(string connectionString)
    : base(connectionString)
    {
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 7:23 PM

All replies

  • User-271186128 posted

    Hi djohannsen,

    Welcome to asp.net forum.

    From your description, I suggest you could refer to this article: http://aspnetmvc.readthedocs.org/projects/mvc/en/latest/migration/migratingconfig.html#migrate-configuration-settings-from-web-config

    To setup configuration using the default MVC 6 settings, add the following constructor and property to the Startup.cs class located in the root of the project:

    public Startup(IHostingEnvironment env)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    
    public IConfigurationRoot Configuration { get; set; }

    Then, add an appsettings.json file to the root of the project and includes the following code:

    {
    	"Data": {
    		"DefaultConnection": {
    			"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"
    		}
    	}
    }

    Best regards,
    Dillion

    Tuesday, February 16, 2016 7:22 AM
  • User-1882553206 posted

    Zhi Lv's solution will not work for Database First running in MVC 6, since need to explicitly pass in connection string into DbContext object directly.  Will post fulling solution.

    Friday, February 19, 2016 7:22 PM
  • User-1882553206 posted

    I was incorrectly relying on EF to pick up the connection string automatically. This worked fine for MVC 5 (and previous versions) running with Database First model where we could specify the expected DB connection name, but with MVC 6 will need to pass in the connection string explicitly.

    I added the additional ctor taking the connection string in its own partial class file, since we're doing Database First and the context file gets overwritten with successive model updates from DB. I.e having in separate partial class / file preserves new ctor to not be overwritten.

    public partial class MyDbContext : DbContext
    {
    public MyDbContext(string connectionString)
    : base(connectionString)
    {
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2016 7:23 PM