Getting database connection string in ASP.NET 5/EF 6

asp.net asp.net-core configuration entity-framework entity-framework-6

Question

I have a solution with two projects: ASP.NET 5 Web project and Class library with EF6. In old ASP.NET 4 world I had DbContext class with the constructor:

public MyModel() : base("name=MyModel") {    }

And then in the web application I had connection string in web.config. I also have a DefaultConnection value that I used for Membership-based identification.

Now I am trying to get to ASP.NET 5. Default Connection string works in the "new" way (I am starting identity from scratch and eventually I will not use DB-Based authentication anyway) - I am getting authenticated properly. Default connection string is in appsettings.json. However, no matter what I tried, I am getting an error No connection string named 'MyModel' could be found in the application config file. I tried changing name=MyModel to name=Data:AAOModel:ConnectionStringas suggested here but I still get the same exception with the new value.

Relevant part of appsettings.json is this (I realize that I can split into config.json - but it doesn't make any difference):

"Data": {
"DefaultConnection": {
  "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=..."
},
"MyModel": {
  "ConnectionString": "Server=dbserver;Database=BusinessDB..."
}

}

I also tried to use ConfigureServices() in Startup the way IdentityContext is added in the scaffolded code and as described here. However, I noticed that IdentityContext is derived from Microsoft.Data.Entity but my MyModel is derived from System.Data.Entity, and services.AddDbContext<>() doesn't like it.

At this time it feels that I tried all permutations that Google can buy. What is the right way to use ASP.NET 5 with EF6 class library?

1
1
5/23/2017 12:07:16 PM

Accepted Answer

You have a couple of fairly easy options:

1: Start using Entity Framework 7.

  • It's release schedule is timed in coordination with Asp.net 5, some features may be missing for awhile though
  • You would then use the AddDbContext as you've already tried

2: You can pass the entire connection string to the constructor/base upon context creation, using Asp.net configuration to pull the value from the json config.

public MyModel(string connectionString) : base(connectionString) { }

  • The base context in EF6 requires either the name of the connection string (to look in configuration) or the actual connection string itself.
0
1/4/2016 3:20:19 AM

Popular Answer

I tried @Ryan's #2 approach and it somewhat working, but instantiating DbContext by passing connection string (or SqlConnection object) will tell EF to create Code First context, which, in my case, EDMX 's classes are not Code First compatible. The EDMX's classes (poco, if you will) must be Code First friendly, otherwise, won't work.

My solution to this is to simply add App.config file in the web project.

I don't like this workaround, but when you have to use EF 6 and ASP.NET 5 web project, you gotta do what you gotta do.

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="SomethingEntities" connectionString="metadata=res://*/Entities.Something.Entities.csdl|res://*/Entities.Something.Entities.ssdl|res://*/Entities.Something.Entities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\;initial catalog=Db_Local;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow