No connection string named 'MyApplicationEntities' could be found in the application config file - connection-string

I just install EF 4.3 and trying to upgrade my project with migration. however I am getting issues with trying to execute add-migration initial to my project via Package Manager console.
It is throwing any exception now No connection string named 'MyApplicationEntities' could be found in the application config file.
Now my config has it all
<connectionStrings>
<add name="MyApplicationEntities"
connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=MyApplicationEntitiesDB;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
I am not sure what is the issue is it a bug in EF 4.3 or there is something I am not doing right.
I thought this post has solved the issue but not quite.
Anyone got an answer.
Appreciate Sanj.

Ah, figured this out accidentally.
I had to remove
public MasterEntities()
: base("name=MyApplicationEntities")
// ^^^^^
{
}
to
public MasterEntities()
: base("MyApplicationEntities")
{
}
EF 4.3 does not like connection string being called name=xxxxx

The solution as Sanj pointed out is that you need to copy the connection string from your database project's App.config to the web project's web.config. I'm not sure why the above answer is marked as correct. I'm adding this as an answer instead of a comment so future readers will spot this.

I had the same error but I already had a web.config file with the correct connection string name and a DbContext declared correctly. However, I noticed when I ran add-migration with -Verbose it state the 'Startup Project' as a different project than the one containing my context. So I change the Startup Project, re-ran the add-migration and it all worked!!

Make sure your statup project config file has the connection string.This link may help you.

I also had this problem and solved it by
Selecting the correct StartUp project.
Rerunning the command on Package Manager Console.
Things worked out as expected.

I also encountered the similar exception. AppConfig is originally gets created in the project that we generate the entity model.
But if you are executing the application using some other project (there are several Projects in my solution), the AppConfig needs to be included in the project which is being executed.

1. ctor => Context
public MasterEntities()
: base("ConnectionStringName")
{
}
2. config file
<add name="ConnectionStringName"
connectionString="Data Source=.;Initial Catalog=DatabaseName;User Id=sa; Password=YourPass;"
providerName ="System.Data.SqlClient" />
3. in Sulation Exporer right click the project and select 'Set as
startup project'
4. in PackageManagerConsole Change Default Project to Your Project of
context class.
5. then:
add-migration new
or added ConnectionString to config file of working Project.

In my case, i got two projects:
I just have to copy the connection string from DAL App.Config project to WPF App.Config project

If you get this error and you're working with Oracle DBs in .NET, check your existing <connectionStrings> tag in the startup project (web.config in web projects, app.config in console/Windows projects), and make sure Oracle didn't replace it with nonsense.
Installing the Oracle.ManagedDataAccess.* assemblies from NuGet does this obnoxious thing where it deletes whatever <connectionStrings> tag that already existed in destination project and replaces it with a new one at the bottom containing a boilerplate Oracle connection string.
Do a git diff or whatever you use to diff changes and focus on your *.config files. Restore old connection strings as needed, then rebuild and try again.
For all others, just remember that the <connectionStrings> tag is only read from the startup project's configuration (aka the entry assembly). If you have your DB stuff in another project that is referenced by your startup project, and you have an app.config in that DB project with a <connectionStrings> tag in it, that tag is only used for for scaffolding EF stuff. After that, the DB project reads the tag from the startup project's configuration instead of its own.

For anyone arriving here because they are getting this error while working with WPF in Visual Studio, please take a look at this post:
Does MVVM stop the ability for the Visual Studio Designer to show xaml?

Related

MicrosoftEntityFrameworkCore is unable find migrations from the assembly

I am using entity framework core Ver. 2.2.4, and I am maintaining a separate assembly for maintaining ef generated migrations. During the application startup, when I try to perform context.Database.Migrate(), I am getting the following message in the output log
No migrations were found in assembly 'MyProject.Core'.
I have included optionsBuilder.UseSqlite(GetConnectionString(), builder => builder.MigrationsAssembly("MyProject.Core")); to load the appropriate assembly to find the migrations, but no luck. Any Idea what's going on?
After looking into the EF Core source code, I figured out what the issue was. The problem was with my application db context class i.e. AppDbContext:DbContext. I had this class in my core project marked as abstract and later I was deriving from the class in the android project to define connection string. Looks like EF Core will ignore all the migrations marked with [DbContext(typeof(AppDbContext))] attribute containing abstract db context types. Once I made my db context class concrete, my migrations started working.
Reference your migrations assembly from your project.
If you can't because it causes a circular dependency, set the output path of your migrations assembly to your main project's directory: (or otherwise copy it there)
<PropertyGroup>
<BaseOutputPath>..\MyStartupProject\bin\</BaseOutputPath>
</PropertyGroup>

Enable-Migrations causes project failed to build

I have created a sample WPF project, worked fine.
I added a MVVMLight project, made it a default startup project, and ran fine.
Now I added a class library project, and added an ADO.NET data model with "Code First From Database", and selected relevant tables, that gave me a good collection of entity classes.
I thought this way, I can keep my data model seperate from presentation and businesss logic layers, plus add additional target plateforms if needed and take the advantage of SoC.
So far so good.
Now I want to enable Migrations on my data layer only. I fired up Nuget console, selected dataLayer project in the "Default Project" drop down. Issued Enable-Migrations command and very first error I got was
"No connection string named 'dpFinDataModel' could be found in the application config file."
Now I modified Startup project - i.e. Mvvm project's App.Config to add App.Config details from DataLayer project.
Tried to rerun "Enable-Migrations", and following error occured:
PM> Enable-Migrations
The project 'dpFinMvvm' failed to build.
Am I doing something wrong here?
Will this approach work?
Thanks for all your help.
-DP
1) PM> Enable-Migrations would get an error if your project itself contains errors.
Try going through your code such as in debug mode and check if you have any build errors then try.
2) And for your config file error you may have not referenced it properly, try this tutorial to solve your issue.
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application

Multiple winforms projects - need to alter app.config files at time of install

I have a winforms solution with multiple projects (UI, web services, data access). All of these projects have their own app.config file. At install time, I need to change the connection string in the data access project based on user's database configuration.
But the data access project creates a dll at install time, with no app.config file available for updating. The UI project is the only project with an app.config file created. And the UI project has a reference to the data access project, so I don't believe the data access project can "see" the config settings of the UI project.
I have searched SO for an answer but have not found anything specific to this situation. Am I doing something wrong in how my projects are set up? Any help would be appreciated.
For anyone else facing this issue:
I believe I found the answer here
In Visual Studio, on the properties of each config file, you can choose "Copy Always" for Copy to Output Directory. This will explicitly create the config file for dll projects.
Then in InstallShield, you can refer to this config file ("MyApplication.dll.config" for example), and alter the connection string through the "Text File Changes" section under "System Configuration". When the user is prompted to connect to a sql server, InstallShield can use those connection properties to alter the connection string in the config file.

Using a test/dev/prod database strategy with Entity Framework

When working in Ruby (specifically, in Rails), I can automatically run my tests using a testing database, and also choose easily between a development or production database. Still new to Entity Framework (WPF), but it seems less than simple.
My Entity assembly has an App.Config file that holds a reference to the database, and I need to copy this App.Config file to all runnable projects (e.g. questions 1113361 and 2233897).
If I use Test->Database Test Configuration without explicitly copying App.Config from my EF project, I get
System.ArgumentException: The
specified named connection is either
not found in the configuration, not
intended to be used with the
EntityClient provider, or not valid.
which is the same as if I have a missing or different App.Config file.
Strangely even if I do change all the connection stings in my Entity project and testing project to the same database, I still get that error.
Is there a step I'm missing when telling Visual Studio (2010 Ultimate) that I want my tests to run using a different testing database, or is this just not supported with EF 4?
Also, Is there some way to change database context other than copying App.Config files back and forth? Seems like a serious way to ignore separation of concerns if not, so I think I'm missing something.
Strangely, as I was playing around after doing this, it started working. Somebody might comment on this, but it looks like I was using Test->Database Test Configuration at the wrong time.
To wit:
Clean project, no App.Config, use Test->Database Test Configuration to set the test database. It generates an app.config (lowercase, notably). You get an error. Even if you check all the connection strings (weird)
Clean project, first copy the App.Config from from you Entity project. Now when you use Test->Database Test Configuration, it will populate the App.Config that you copied, and it works.
I imagine it was some issue I was creating when copying the connection strings.
I've also confirmed that this works with my Entity project using a "Main" database while my testing project uses a "Test" database.
Wonder if someone can confirm/clarify this?

Choosing connectionstring in VB.NET application at startup

I have a VB.NET application with a connection to an SQL Server 2003. On the server there are two databases, MyDatabase and MyDatabase_Test. What I would like to do is to show a dialog when the program starts that let's the user choose which database to use. My idea is to create a new form as the starup form that sets this property and then launches the main form.
Currently the connectionstring is specified in the application config file. Best would be if I can specify two different connection strings in that file to choose from, but for now it is also acceptable with other solutions like hardcoding the two connectionstrings into the startup form.
EDIT: In the dataset.xsd file there seems to be the relevant part
<Connections>
<Connection AppSettingsObjectName="MySettings" AppSettingsPropertyName="MyDatabase_ConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="MyDatabase_ConnectionString(MySettings)" ParameterPrefix="#" PropertyReference="ApplicationSettings.MyProgram.My.MySettings.GlobalReference.Default.MyDatabase_ConnectionString" Provider="System.Data.SqlClient" />
</Connections>
But how do I change it at runtime? The closest i could find is changing which connection is used for every single TableAdapter but that doesn't seem very optimal.
EDIT2: I agree that a modal dialog at startup would be better, but where would i launch it so that it is done before the database connection is initiated?
EDIT3: Eventually I "solved" it by removing the ReadOnly from the settings file. This will be removed each time the file is auto-generated though, so it's not optimal.
EDIT4: A better solution seemed to be using a user scoped string instead of a connection string to link the dataset and fetched the value for that string from the two application scoped ConnectionStrings.
You could just set the TableAdapter.Connection.ConnectionString property right before you use it every time.
Find the section in your app.config which defines the connection strings and add another:
<connectionStrings>
<add name="Live"
connectionString="Data Source=svr;Initial Catalog=Live;..."
providerName="System.Data.SqlClient" />
<add name="Dev"
connectionString="Data Source=svr;Initial Catalog=Dev;..."
providerName="System.Data.SqlClient" />
</connectionStrings>
on startup, populate a global variable that reads out of one setting or the other based on the users choice
After some more consideration hacking the settings file to remove the ReadOnly property seems like the best solution.
It is not possible to define a ConnectionString at the user scope, only at the application scope. It is possible to use a string instead of a ConnectionString, the program will run fine, but it causes a lot of IDE issues and Visual Studio exceptions during the auto-compiling.
I don't understand the question, or rather I can't see any question.
Are you having any specific problems with doing this or just wondering if it's an ok design?
Having multiple connection strings in the config file and then choosing between them at startup should work fine. The only thing I might do different from how you describe it that I'd probably keep the main form as the startup form and then do something like pop up a modal dialog box straight away where the user selects the connection.

Resources