Felix
Felix

Reputation: 10078

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

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?

Upvotes: 1

Views: 2418

Answers (3)

AllanB
AllanB

Reputation: 161

The simplest way I've found is just adding a key in the web.config with the connection string which is accessible via the ConfigurationManager.

 <add key="ConnectionString" value="..." />

System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());

Upvotes: 0

stack247
stack247

Reputation: 5747

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>

Upvotes: 2

developer
developer

Reputation: 113

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.

Upvotes: 0

Related Questions