views:

433

answers:

1

Hi,

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 said "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)

A: 

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.

Craig Stuntz
But I'm not adding a plant, I'm simply changing its main information. It is all binded to the window so I don't think anything is missing, I tried looking(seeing if before saving changes there was a null reference to anything) and there's nothing missing that I can see... Can you give me advice on how to check if something is missing?
sklitzz
Oh, I see. Your binding is probably wrong, then. It looks like the linked control is causing an insert rather than an update, probably because the key isn't detected? I'm no expert on that, though. Have you tried using the Entity Data Source? It's supposed to take care of this for you.
Craig Stuntz