Connection string cannot be found in startup.cs? Is this a bug?
Problem
Steps to duplicate problem
- Go to the directory of the application in package console
- k ef migrations add initial
Startup.cs and SampleContext.cs files
In fact, we put up a repo for you to see our sample application showing this problem: https://github.com/HeathWillCode/SampleDbContext
Notes: Yes, we could hardcode the connection string in SampleContext.cs, but we feel it should be straightforward to configure the startup.cs file to pick up the connection string. Honestly, we have gotten it work for this application found here: https://github.com/HeathWillCode/vNextSampleApplication but really unsure as to what, if anything was different in our implementation except the connection string itself. Which is really strange because the connection string we use in the SampleDbContext is the auto-generated one?
Thanks for taking a look at this and please let us know what your thoughts are.
Hey,
The issue is the options.UseSqlServer() call in OnConfiguring. This call is overriding the UseSqlServer call you have in Startup.cs where you specify the connection string. If no connection string is specified then we try to load one from the EntityFramework section in config.
Opened https://github.com/aspnet/EntityFramework/issues/1473 to look at making this issue harder to hit.
~Rowan
"If no connection string is specified then we try to load one from the EntityFramework section in config." Rowan, I think may be broken...
If you call UseSqlServer() from either the Startup.cs or the MyDBConxext.cs UseSqlServer() can't seem to find the connection string in the Config.json please see this repo that demonstrates this: https://github.com/HeathWillCode/SampleDbContext
@WillBaldwin - In that repo you linked to there is no connection string for the context specified in config.json. You would need to have something like this in your config.json:
"entityFramework": {
"SampleContext": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=FooBarDB;Trusted_Connection=True;"
}
}
@rowanmiller ,
I have tested your Config.json settings and they work! No surprise to you, I'm sure.
I have a suggestion; when Visual Studio creates a Config.json file it places a default connection string in "Data:DefaultConnection:ConnectionString"; EF7 is looking for "entityFramework:MyDbContext:ConnectionString". I suggest that you change EF7 to look for the "Data:DefaultConnection:ConnectionString" as the "general or normal case" and if the developer needs a separate connection string for EF that would be the "special case" rather than the other way around. It just seems to be that 99% of the EF7 projects out there will only need a single connection string in the project. Even if you mix Entity Framework and Identity Framework in the same project I am finding that the general solution uses only one connection string.
@WillBaldwin - we're trying to do away with as much magic as possible around connection strings in EF7. BTW the new project templates for ASP.NET5 include the EntityFramework sections already scaffolded for the Identity context.
@rowanmiller I appreciate your reply and sharing the thinking behind decisions being made. We, too, are fans of doing away with magic. Your teams work on migrations is really evidence of this. Huge fans of the changes to it.
You stated that the team is trying to do away with as much magic around connection strings. We feel there must be a reason that you mentioned magic in this instance (as you did with Mr. Hanselman during a Channel 9 show about migrations). Could you elaborate please?
You mentioned the ASP.NET 5 template code. This is an excellent thing to bring up because it seems to me that we can code for one connection string instead of two. We view ASP.NET 5 modular components like Lego's. So we feel confused as to why VS15 creates a default string and doesn't have EF (when brought into the application) pick it up automatically like snapping together like Legos.
The converse way to consider the above is to have a k command to generate config.json just like we can for the controller/view of a model. Instead, in this instance we are allowed to connect it to EF in a command as an option. If you like that suggestion, please let us know who we should discuss that with.
Thanks for your time @rowanmiller
@heathyates - I mentioned 'magic' because having EF special case a connection string called 'DefaultConnection' is pretty magic. Actually in ASP.NET 5 and config.json there is no concept of connection string... so it would actually be special casing the data:defaultConnection:connectionString config value.
Another data point is that we would have gotten rid of defaultConnection in ASP.NET 5 except that this is special cased for Azure deployment (yes... that is magic :smile:... and I despise it). What you will see in the project templates is that we support specifying a different config key for the connectionString element. So config looks something like this:
{
"data": {
"defaultConnection": {
"connectionString": "Server=(localdb)\\MSSQLLocalDB;Database=FooBarDB;Trusted_Connection=True;"
}
}
"entityFramework": {
"SampleContext": {
"ConnectionString": "name=data:defaultConnection:connectionString"
}
}
}
We were actually considering whether having EF automatically wire up from the entityFramework section was potentially too implicit and it may be better to have folks tell the context exactly what config value to use for the connection string (but we ultimately decided that having a framework specific section is a well known pattern).
Thank you for the time you kindly put into responding. Your explanation has helped me understand entity framework better from an architectural standpoint, and for that, I cannot thank you enough for responding.