Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2265
Entity Framework Multiple Band Issue with Parent/Child relationships
posted

I am binding my UltraGrid to an Entity Framework (nHydrate) data source (System.Windows.Forms.BindingSource).  So for examples sake:

ClassA (Band 0)
    ClassB (Band 1)

I can add rows to Band 0 without any problems using the Fixed Header Row.  I am using the AllowAddNew = FixedAddRowOnTop to do this.  I am doing the same with Band 1.

However, when I add a new row in Band 1, a new instance of ClassB is created which I can see in the AfterRowUpdate event.  However, the relation between ClassA and ClassB is never set and if I try to save the context I get the error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

If I manually try to set the relationship either by adding CType(e.Row.ParentRow.ListObject, ClassA).ClassB.ToList().Add(<the new ClassB row> ), then I get an extra row in the grid in Band 1.

This is frustrating me as I have spent a lot of late nights trying to work around this.  I cannot see any way to proceed with the grid - I hope I am wrong.

Any thoughts?

Thanks

Andez

 

 

 

 

Parents
  • 469350
    Offline posted

    Hi Andez,

    I'm not very familiar with the Entity framework. But it seems to me that the line of code you added here to set the relationship is not correct. You are adding a new row to band 1 instead of using the existing row that is already created.

    What you probably need to do is access the ListObject of the AddNew row in the grid and that should give you the data object (Class B) that corresponds to that row in the grid.

    I'm not sure how you establish the relationship between elements in the EntityFramework, though, so I might be wrong.

Reply Children