This is driving me mad. I'm fairly new to WPF/EF.
I have a simple MVVM app which reads an Entity Table into a DataGrid via binding in the XAML. The app compiles fine.
I am getting this unhandled exception however which locks the designer:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
The XAML cannot create an instance of my View Model...
xmlns:vm="clr-namespace:Entity_MVVM"
Title="MainWindow" Height="600" Width="800"
DataContext="{DynamicResource MyViewModel}">
<Window.Resources>
<vm:CountrysViewModel x:Key="MyViewModel"/>
</Window.Resources>
Here is my View Model 'Load Grid' method:
public void LoadGrid()
{
var db = new LDBEntities();
using (var conn = new EntityConnection("name=LDBEntities"))
{
conn.Open();
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT VALUE c FROM LDBEntities.tbCountrys as c";
try
{
EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection);
_CountrysModelObservableList.Clear();
while (rdr.Read())
{
var cCountryId = rdr["CountryId"].ToString();
var cShortName = rdr["shortName"].ToString();
var cLongName = rdr["longName"].ToString();
_CountrysModelView = new CountrysModel()
{
CountryId = cCountryId,
ShortName = cShortName,
LongName = cLongName
};
_CountrysModelObservableList.Add(_CountrysModelView);
}
}
catch(Exception e)
{
MessageBox.Show(string.Format("Can't read in data!"));
}
}
The connection string in my App.config was created on creation of my EF model and populates the DataGrid as intended.
Any ideas what is causing this?
Friday afternoon frustration! Thanks
Edit: App.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="LDBEntities" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=DMEA-T1000\SQLEXPRESS;Initial Catalog=LDB;Persist Security Info=True;User ID=sa;Password=PasswordHidden;MultipleActiveResultSets=True' " providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>