none
Entity Framework Fixup Exception thrown RRS feed

  • Question

  • I am using Entity Framework 4.0 and POCO's. having trouble describing problem so will give an example.

    I have an entity Case which has a many to one relationship with CaseStatus.

    The entityobject for Case has properties caseStatusId and CaseStatus. When I create a new Case I set a default value for the caseStatusId, but CaseStatus is still null.

    When I add the Case entity to the Context and the SaveChanges I get an InvalidOperationException on the fixup for the CaseStatus

    if (!CaseStatus.Cases.Contains(this)), it states the Case already exists in the Cases Collection.

    However instead of setting the caseStatusId, I set a default value to the CaseStatus property in the Case it all works fine, is this how it is supposed to be as I would rather just set the Id value as it is less work. Also for some entitys it wil be confusing as if I had multiple CaseStatus properties int Case they are named something like CaseStatus1, CaseStatus2 which will become confusing considering the id properties can be descriptive.

    Thanks

    Simon.


    Simon

    Wednesday, April 18, 2012 5:51 AM

Answers

  • Hi Simon,

    Welcome!

    If you include the Foreign key and Navigations in your code. I think insert by FK or navigation are same. Based on your description, youo set a default value for caseStatusId, I'm not sure about the default value here. I think you should set the Id of CaseStatus that is exist in database.

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 19, 2012 8:48 AM
    Moderator
  • Seems my XAML displaying the CaseStatus in a combo box is causing the issues, as it worked if I removed my bindings to the combobox.

    ItemsSource is an observablecollection<CaseStatus> which is a collection of all case statuses, SelectedValuePath="CaseStatusId" I presume CaseStatusId here points to a property in CaseStatus in the ItemsSource.

    Perhaps I should post this in the WPF forums now.

    <ComboBox Grid.Row="1" Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" VerticalContentAlignment="Center"
                      DisplayMemberPath="CaseStatusDescription"
                      ItemsSource="{Binding CaseStatuses}"
                      SelectedValue="{Binding CaseDetail.CaseStatusId,
                                              ValidatesOnDataErrors=True,
                                              ValidatesOnExceptions=True,
                                              NotifyOnValidationError=True,
                                              UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="CaseStatusId"
                      Style="{DynamicResource BaseComboBoxStyle}"
                      Validation.ErrorTemplate="{StaticResource InputErrorTemplate}" />


    Simon

    Thursday, April 19, 2012 11:15 PM

All replies

  • Hi Simon,

    Welcome!

    If you include the Foreign key and Navigations in your code. I think insert by FK or navigation are same. Based on your description, youo set a default value for caseStatusId, I'm not sure about the default value here. I think you should set the Id of CaseStatus that is exist in database.

    Have a nice day. 


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 19, 2012 8:48 AM
    Moderator
  • Seems my XAML displaying the CaseStatus in a combo box is causing the issues, as it worked if I removed my bindings to the combobox.

    ItemsSource is an observablecollection<CaseStatus> which is a collection of all case statuses, SelectedValuePath="CaseStatusId" I presume CaseStatusId here points to a property in CaseStatus in the ItemsSource.

    Perhaps I should post this in the WPF forums now.

    <ComboBox Grid.Row="1" Grid.Column="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" VerticalContentAlignment="Center"
                      DisplayMemberPath="CaseStatusDescription"
                      ItemsSource="{Binding CaseStatuses}"
                      SelectedValue="{Binding CaseDetail.CaseStatusId,
                                              ValidatesOnDataErrors=True,
                                              ValidatesOnExceptions=True,
                                              NotifyOnValidationError=True,
                                              UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="CaseStatusId"
                      Style="{DynamicResource BaseComboBoxStyle}"
                      Validation.ErrorTemplate="{StaticResource InputErrorTemplate}" />


    Simon

    Thursday, April 19, 2012 11:15 PM
  • Hi Simon,

    Thanks for your feedback.

    Good Luck!

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 20, 2012 1:27 AM
    Moderator