Code first migrations - what connection string will it use?
A

2

15

Code first migrations have been working very well for me. I have a services project and a wpf project. The model is in the services project which is referenced by the wpf project. Update-database is done on the services project, but uses connection string from the wpf project. I now add a web project which also references the service project. So now that there is a connection string in the app.config and there is one in the web.config, which one will it use?

Armistice answered 3/2, 2014 at 16:38 Comment(0)
A
40

In my scenario, the app.config in the services project is ignored. Code first migrations will use either the app.config from the WPF project or the web.config on the web project, depending which is selected as the startup project.

Armistice answered 4/2, 2014 at 15:9 Comment(5)
@Shumii, thanks! You comments saved me. When I was trying to enable migration, I always have the error "The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections." Actually the connection was correct. The Problem was because solution's startup project is setting to a project that don't have app.config. After set start up project to the project that contains correct app.config, migration works!Latrice
Wow. Didnt even think about this being the reason. Nice find!!Annecy
Maddening... had no idea that the startup project would affect what happens in package manager console. Thanks.Ferguson
You can use the -ProjectName, -StartupProjectName, -ConnectionStringName, and -ConnectionString parameters to override the default behavior. See #25014290.Scheldt
This is ridiculous. I added a new worker role project and by default it was set as startup project. Now whenever I did add-migration it would create all the tables in my Context from scratch and i had no clue. Your answer saved my day :)Threshold
A
5

When doing update-database you should specify the project that contains the migrations. Make sure that you have an app.config file in that project that contains the correct connection string.
you can do a Update-Database -ConnectionStringName "MyConnectionString" and it should work like a charm.

Alvinalvina answered 3/2, 2014 at 18:1 Comment(7)
What you said is correct and follows official documentation. However, in my case the app.config in the services project is being ignored, and when specifying a connection string it tells me it does not exist. The only connection strings it sees is in the app.config of the WPF project. I assume the WPF app.config overrides that of the services project. Doesn't explain why the connection strings in the web.config are completely ignored.Armistice
you can set name of connection string as full namespace <add name="YourClassProject.EfDbContext" ... Alvinalvina
or you can create your DbContext with your connection string name passing to the constructor : public EfDbContext(): base("nameOfYourConnectionString")Alvinalvina
Thanks, I am aware of these different ways to do it - but my question is asking what will happen in a very specific scenario when you do not state the connection name and leave it to default.Armistice
if you don't state the connection string name it uses a connection string name that its name matches with your context name.Alvinalvina
everything you are saying is true, but still did not answer my question.Armistice
@SirwanAfifi, you seem to miss Shumii's point. It's not about what string name, but about what config file. The same name can occur in many config files, so merely specifying the name isn't the answer.Gayden

© 2022 - 2024 — McMap. All rights reserved.