2013-10-11 58 views
0

这让我很生气。我对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> 
+0

什么版本的EF? 4? – Charleh

+0

嗨,是的EF4,感谢 – Hardgraf

+3

国际海事组织,使用像这样的实体框架(就像它是与所有这些魔术字符的东西和手动映射普通旧ADO.Net)完全失败了ORM的目的。 –

回答

1

当您ERM添加到项目它会为你创建模型。

如果你在你的数据库的表中调用例如tblYears你应该能够声明:

tblYear y = new tblYear(); 

我个人创建本地模型,并填充它的观点,即视图模型使用。

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 
} 

然后,您可以使用此视图模型要么填充列表<>或作为一个单独的记录 - 列表例如:

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

+0

太好了,谢谢你的帮助!只有1年的编程/ C#经验,这大大简化了事情。 – Hardgraf

+0

我似乎无法为MyERM.TableName创建一个对象(在我的情况下为LDBEntities.tbCountrys) – Hardgraf

+0

如果您在添加erm实例后将其添加到数据库,则需要刷新erm。如果刷新不起作用(buggy),请复制名称,删除它并使用相同的名称重新添加它。然后数据库中的任何更新都会反映在erm中。我必须多次这样做,这是习惯习惯。更改数据库 - 重新附加edm(如果刷新不起作用)。 –