[英]MVVM in WPF with Entity Framework unhandled exception
這讓我發瘋。 我是WPF / EF的新手。
我有一個簡單的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>
這是我的視圖模型“加載網格”方法:
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中的連接字符串是在創建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>
將ERM添加到項目時,它將為您創建模型。
例如,如果數據庫中有一個名為tblYears的表,則應該可以聲明:
tblYear y = new tblYear();
我個人創建了一個本地模型,並將其填充到視圖中使用,即viewmodel。
class YearModel : INotifyPropertyChanged
{
#region Members
MyERM.tblYear _year;
#endregion
#region Properties
public MyERM.tblYear Year
{
get { return _year; }
}
public Int32 id
{
get { return Year.id; }
set
{
Year.id = value;
NotifyPropertyChanged("id");
}
}
public String Description
{
get { return Year.Description; }
set
{
Year.Description = value;
NotifyPropertyChanged("Description");
}
}
#endregion
#region Construction
public YearModel()
{
this._year = new MyERM.Year
{
id = 0,
Description = ""
};
}
#endregion
}
然后,您可以使用此視圖模型填充List <>或作為單個記錄-列表示例:
class YearListModel
{
myERM db = new myERM();
#region Members
private ObservableCollection<YearModel> _years;
#endregion
#region Properties
public ObservableCollection<YearModel> Years
{
get { return _years; }
}
#endregion
#region Construction
public YearListModel()
{
_years = new ObservableCollection<YearModel>();
foreach (MyERM.tblYear y in db.tblYears())
{
_years.Add(new YearModel
{
id = y.id,
Description = y.Description
}
);
}
}
#endregion
}
然后,例如,您可以將其發送到如下頁面:
xmlns:local="clr-namespace:MyProject.ViewModels"
<Page.Resources>
<local:YearListModel x:Key="YearList" />
</Page.Resources>
並將其綁定到控件:
<ListView x:Name="listviewname"
DataContext="{StaticResource ResourceKey=YearList}"
ItemsSource="{Binding Path=Years}">
<ListView.View>
<GridView>
<GridViewColumn x:Name="columnname" Header="Code"
DisplayMemberBinding="{Binding Code}"/>
</GridView>
</ListView.View>
</ListView>
希望這對GL有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.