Entity Framework 4 を使用して VS2010 .NET4.0 で単純な mvvm WPF アプリを構築しています。プログラミング経験が 1 年しかない WPF 初心者です。XAML のデータグリッドを Entity Framework モデルにバインドしようとしています。View Model に監視可能なコレクションがありますが、データを読み込めないようです。
プロジェクトには、プライマリ ID を持つ単一の「tbCountrys」テーブルを保持する Entity Framework .edmx が含まれています。
これは、テーブルの列に関連するいくつかの変数/プロパティを宣言し、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 も実装し、Observable Collection を宣言してクエリ結果を格納し、LoadGrid() メソッドを使用してテーブルに EntityConnection をクエリし、Observable Collection を設定します。これは機能していないようですか?ビュー モデルのコンストラクターから LoadGrid() メソッドを呼び出しています。
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 ブロックが実行されず、catch 例外がスローされます。EntityDataReader オブジェクトを宣言した後、データを読み込む while ループが実行されません。クエリ構文が正しくない可能性がありますか?
MVVM で Entity Framework を使用する例をオンラインで見つけることができないため、どんな助けも大歓迎です。ありがとう
また、XAML でこの例外を取得すると、View Model のインスタンスを作成できません。接続文字列はApp.configで正しいので、何が原因かわかりません...