Donate

WPF MVVM - No connection string named 'MyEntities' could be found in the application config file.

Hello Team,

While working on a simple WPF MVVM application using Entity Framework 6.x and Visual Studio 2019, I encountered this strange error No connection string named 'MyEntities' could be found in the application config file. However, if I compile and debug my application, it would proceed with running the project. I've searched lots of information and most of the solutions presented were to copy the web.config or app.config file from a class library to the project that is set as the startup application. One thing I've noticed though is that this error appears only on the XAML page of where the DataContext is assigned. After doing some experiments of modifying my application, I came up with a solution that is instead of setting the DataContext in the XAML like below:
<Window.DataContext>
    <viewModel:StudentViewModel/>
</Window.DataContext>
I assigned the DataContext in the code behind of the MainWindow and thus the error message disappeared.
public partial class MainWindow : Window
{
	public MainWindow()
	{
		InitializeComponent();
		this.DataContext = new StudentViewModel();
	}
}
Note that this is only a temporary workaround since the common method is to set the DataContext in the XAML page. If this is a bug, I hope Microsoft will release a hotfix for this to resolve this strange issue.
WPF MVVM - No connection string named 'MyEntities' could be found in the application config file in Entity Framework

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations