我正在用Entity Framework 4在VS2010 .NET4.0中構建一個簡單的mvvm WPF應用程序。我是一個只有1年編程經驗的WPF初學者。嘗試將我的XAML中的數據網格綁定到實體框架模型。我的View Model中有一個可觀察的集合,但似乎無法讀取數據?帶實體框架的WPF MVVM?
我有一個包含在項目中的實體框架.edmx,它包含一個帶有主ID的單個「tbCountrys」表。
這裏是剛剛宣佈了一些變量/與我的表列的屬性和實現INotifyPropertyChanged我的模型類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace Entity_MVVM
{
public class CountrysModel : INotifyPropertyChanged
{
#region variables
private string _CountryId;
private string _ShortName;
private string _LongName;
# endregion
# region Properties
public string CountryId
{
get { return _CountryId; }
set { _CountryId = value;
OnPropertyChanged("CountryId");
}
}
public string ShortName
{
get { return _ShortName; }
set
{
_ShortName = value;
OnPropertyChanged("ShortName");
}
}
public string LongName
{
get { return _LongName; }
set
{
_LongName = value;
OnPropertyChanged("LongName");
}
}
#endregion
# region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
# endregion
}
}
我的視圖模型也實現INotifyPropertyChanged,宣稱可觀察集合來存儲我的查詢結果,有一個LoadGrid()方法,它應該用EntityConnection查詢我的表並填充Observable集合。這似乎沒有工作?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data;
using System.Windows;
using System.Collections;
namespace Entity_MVVM
{
public class CountrysViewModel : INotifyPropertyChanged
{
#region Constructor
public CountrysViewModel()
{
LoadGrid();
}
# endregion
# region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
# endregion
# region ObservableCollection
private ObservableCollection<CountrysModel> _CountrysModelObservableList = new ObservableCollection<CountrysModel>();
public ObservableCollection<CountrysModel> CountrysModelObservableList
{
get { return _CountrysModelObservableList; }
set
{
_CountrysModelObservableList = value;
OnPropertyChanged("CountrysModelObservableList");
}
}
# endregion
# region Properties
private CountrysModel _CountrysModelView;
public CountrysModel CountrysModelView
{
get { return _CountrysModelView; }
set
{
_CountrysModelView = value;
OnPropertyChanged("CountrysModel");
}
}
# endregion
# region LoadGrid Method
public void LoadGrid()
{
LDBEntities db = new LDBEntities();
using (var conn = new EntityConnection("name=LDBEntities"))
{
conn.Open();
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM LDBEntities.tbCountrys";
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
{
MessageBox.Show(string.Format("Can't read in data!"));
}
}
#endregion
}
}
}
最後,我的XAML視圖:
<Window x:Class="Entity_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Entity_MVVM"
Title="MainWindow" Height="350" Width="525"
DataContext="{DynamicResource MyViewModel}">
<Window.Resources>
<vm:CountrysViewModel x:Key="MyViewModel"/>
</Window.Resources>
<Grid>
<DataGrid Width="400" Height="127" Name="grdPublications" ItemsSource="{Binding Path=CountrysModelObservableList}">
</DataGrid>
</Grid>
</Window>
當我調試的代碼,在我的視圖模型try塊是我從我的視圖模型的構造函數調用LoadGrid()方法未執行並引發catch異常。讀取數據的while循環從不運行,因爲它在聲明EntityDataReader對象後退出。也許有一些不太正確的查詢語法?
任何幫助將是最受讚賞的,因爲我無法找到許多使用實體框架與MVVM的在線示例。由於
而且,得到這個例外在XAML,不能創建我的視圖模型的實例。連接字符串在App.config中是正確的,所以不知道是什麼導致它...
任何你爲什麼使用實體框架的原因 - 然後手搖你的SQL和使用數據讀取器。爲什麼不直接使用LINQ查詢實體上下文? –
嗨理查德。我試圖找出一個由已使用這種方法讀取數據的離開開發人員編寫的應用程序。我之前在一個代碼隱藏的WPF應用程序中使用了LINQ,並且認爲它更直接。 – Hardgraf