开发者

Entity Framework UpdateException relationships in Added and Deleted states

I have a table of Plants and Information.

Plants have an ID, Name, ..., MainInformationID

Information have an ID, Title, ..., PlantID

One plant can have many information, but only one MainInformation. Information can only have one plant.

I have a class ViewModel which contains observable collections of plants and information which are binded to the window.

When I try to change the main information of a plant. I get this error:

A relationship from the 'FK_Plants_Information' AssociationSet is in the 'Added' state. Given multiplicity constraints, a corresponding 'Information' must also in the 'Added' state.

If I choose an information which is not used by any plant it s开发者_运维百科aid "Added" state and if I choose one which is in use it says "Deleted" state.

**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");
        }
    }
 }

And the window:

<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 looks like this.

    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);
    } 
    }

Also please do tell me if there is anything here that doesn't seem like a good idea(like having a static context, the way I connect my viewModel to the dal and so on... (I'm a beginner so I don't really know how it should be used properly)


The error says you have a required field missing. I'm guessing it's MainInformation. You could do something like:

Dal.AddPlant(new Plant { MainInformation = new Information() });

Yes, it's a bad idea to use a static context. There's some discussion on how to choose an appropriate lifetime in this post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜