這讓我很生氣。我對WPF/EF相當陌生。WPF中的MVVM與實體框架未處理的異常
我有一個簡單的MVVM應用程序,它通過XAML中的綁定將實體表讀入DataGrid。該應用程序編譯好。
不過,我得到這個未處理的異常,其鎖定設計師
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)
的XAML無法創建我的視圖模型的實例...
xmlns:vm="clr-namespace:Entity_MVVM"
Title="MainWindow" Height="600" Width="800"
DataContext="{DynamicResource MyViewModel}">
<Window.Resources>
<vm:CountrysViewModel x:Key="MyViewModel"/>
</Window.Resources>
這裏是我的視圖模型「加載Grid'方法:
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!"));
}
}
我的App.config中的連接字符串是在cre上創建的我的EF模型並根據預期填充DataGrid。
任何想法是什麼造成這種情況?
週五下午無奈!由於
編輯: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>
什麼版本的EF? 4? – Charleh
嗨,是的EF4,感謝 – Hardgraf
國際海事組織,使用像這樣的實體框架(就像它是與所有這些魔術字符的東西和手動映射普通舊ADO.Net)完全失敗了ORM的目的。 –