我有一张植物和信息表。
植物有ID,名字,.,MainInformationID
信息有一个ID,标题,.,PlantID
一个植物可以有很多信息,但只有一个MainInformation。信息只能有一个工厂。
我有一个类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}" />达尔长得像这样。
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的方式,等等)。(我是初学者,所以我不知道该如何正确地使用它)
发布于 2010-03-18 16:04:12
错误说明您丢失了一个必需的字段。我猜是MainInformation。你可以这样做:
Dal.AddPlant(new Plant { MainInformation = new Information() });是的,使用静态上下文是个坏主意。有一些浅谈如何在这篇文章中选择合适的生命周期。
https://stackoverflow.com/questions/2471262
复制相似问题