如何在ViewModel.AddNewMovie()中獲得所選語言的Id? 這是我第一次嘗試。這很糟糕,因爲它是索引,而不是Id。組合框和實體框架,如何獲取ID? (WPF-MVVM)
查看:
...
<ComboBox ItemsSource="{Binding LanguagesCollection}"
SelectedIndex="{Binding NewMovieLanguage}"
DisplayMemberPath="L_Name" SelectedValuePath="L_Id"/>
...
視圖模型:
...
public ObservableCollection<Languages> LanguagesCollection { get; private set; }
public ObservableCollection<Movies> MoviesCollection{ get; private set; }
...
_Entities = new MyAppEntities();
LanguagesCollection = new ObservableCollection<Languages>(_Entities.Languages);
MoviesCollection = new ObservableCollection<Movies>(_Entities.Movies.Include("Languages"));
...
private void AddNewMovie()
{
OnPropertyChanged("NewMovieTitle");
OnPropertyChanged("NewMovieLanguage");
try
{
Movies NewMovie = new Movies
{
M_Title = NewMovieTitle,
M_Language_Id = Convert.ToInt32(NewMovieLanguage)+1,
};
_Entities.AddToMovies(NewMovie);
MoviesCollection.Add(NewMovie);
...
}
catch
{
...
}
}
實體的DataModel:
電影(表)
M_ID 的Int32
M_Title 字符串
M_Language_Id 的Int32 FK
語言(表)
L_Id 的Int32
L_Name 字符串
- 數據 -
語言
1英語
2德國
3法國
4西班牙語