2013-10-10 59 views
0

我正在用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的在線示例。由於

Also, getting this exception in the XAML, can't create an instance of my View Model. The connection string is correct in the App.config so not sure what's causing it...

而且,得到這個例外在XAML,不能創建我的視圖模型的實例。連接字符串在App.config中是正確的,所以不知道是什麼導致它...

+1

任何你爲什麼使用實體框架的原因 - 然後手搖你的SQL和使用數據讀取器。爲什麼不直接使用LINQ查詢實體上下文? –

+0

嗨理查德。我試圖找出一個由已使用這種方法讀取數據的離開開發人員編寫的應用程序。我之前在一個代碼隱藏的WPF應用程序中使用了LINQ,並且認爲它更直接。 – Hardgraf

回答

2

你連接字符串看起來確實錯了。 代碼失敗在EntityConnection構造

at System.Data.EntityClient.EntityConnection.ctor(String connectionString) 

所以這是這一行:

new EntityConnection("name=LDBEntities") 

什麼樣的連接字符串的是"name=LDBEntities"

我想你已經忘記從你的資源文件中獲取連接字符串。

new EntityConnection(SomeResource.LDBEntities) 
+1

連接字符串是在App.config中用name =「LDBEntities」後跟實際字符串聲明的。當然我不需要在c#中聲明它? – Hardgraf

+0

我現在修復了這個問題,我的eSQL查詢不正確,改爲... conn.Open(); EntityCommand cmd = conn.CreateCommand(); cmd.CommandText =「SELECT VALUE c FROM LDBEntities.tbCountrys as c」; – Hardgraf

+0

不,您不需要複製連接串。您必須向構造函數提供資源LDBEntities的實際值。 –