植物と情報の表があります。
植物にはID、名前、...、MainInformationIDがあります
情報にはID、タイトル、...、PlantIDがあります
1つのプラントに多くの情報を含めることができますが、MainInformationは1つだけです。情報には1つのプラントしか含めることができません。
ウィンドウにバインドされている植物と情報の観察可能なコレクションを含むクラスViewModelがあります。
植物の主な情報を変えようとすると。このエラーが発生します:
'FK_Plants_Information'AssociationSetからの関係は'追加'状態です。多重度の制約がある場合、対応する「情報」も「追加」状態である必要があります。
どのプラントでも使用されていない情報を選択すると「追加」状態になり、使用中の情報を選択すると「削除済み」状態になります。
**class PlantsViewModel : ViewModelBase
{
private DAL _dal = new DAL();
private ObservableCollection<Plant> _plants = new ObservableCollection<Plant>();
public ObservableCollection<Plant> Plants
{
get { return _plants; }
set
{
_plants = value;
_OnPropertyChanged("Plants");
}
}
private Plant _selectedPlant;
public Plant SelectedPlant
{
get { return _selectedPlant; }
set
{
_selectedPlant = value;
_OnPropertyChanged("SelectedPlant");
}
}
private ObservableCollection<Information> _information = new ObservableCollection<Information>();
public ObservableCollection<Information> Information
{
get { return _information; }
set
{
_information = value;
_OnPropertyChanged("Information");
}
}
}
そしてウィンドウ:
<TextBox Grid.Column="1" Grid.Row="0" Width="150" Text="{Binding Path=SelectedPlant.LatinName}" />
<TextBox Grid.Column="1" Grid.Row="1" Width="150" Text="{Binding Path=SelectedPlant.LocalName}" />
<TextBox Grid.Column="1" Grid.Row="2" Width="150" Text="{Binding Path=SelectedPlant.CycleTime}" />
<ComboBox Grid.Column="1" Grid.Row="3" Width="150" ItemsSource="{Binding Path=Information}"
DisplayMemberPath="Title"
SelectedValue="{Binding Path=SelectedPlant.MainInformation, Mode=TwoWay}" />
DALはこんな感じ。
public class DAL {
private static Entities _context = new Entities();
public void SaveChanges()
{
_context.SaveChanges();
}
public List<Plant> GetAllPlants()
{
return _context.Plants.ToList();
}
public void AddNewPlant(Plant plant)
{
_context.Plants.AddObject(plant);
}
public void DeletePlant(Plant plant)
{
_context.DeleteObject(plant);
}
}
また、ここに良いアイデアとは思えないものがあるかどうか教えてください(静的なコンテキスト、viewModelをdalに接続する方法など...(私は初心者なので、それがどのように適切に使用されるべきか本当にわかりません)