none
Confused about relations, why cant i update? RRS feed

  • Question

  • This has been troubling me for a week now. Ive read a book worth of articles, and still cant find an answer.

     

    Can somone please explain to me how to properly set up this model to allow CRUD operations????

     

    Categories     ->     Products

     

    CatID                    ProdID

    CatName               ProdName

    CatID

     

    I have a Tree(list) with categories, a Grid(list) with products, and couple of controls below(single) to show a product, ONE of them being a Combobox(CatID) to show a Categories Lookuplist.

     

    When i scroll the tree, grid shows correct, and controls show correct, all i want to do is for a particular Product to change its Category? is that so much to ask, with linq to sql. it aparently is.

     

    Using datasets i could do that in like 3 minutes.

     

     

     

     

     

     

    Saturday, November 29, 2008 10:05 AM

Answers

  • Bind the comboBox to the Category property via the SelectedItem.  Fill the combo box with Category instances.  You'll also have to override ToString on Category to return the text you want to show in the combo box.
    Monday, December 1, 2008 5:09 PM
    Moderator

All replies

  • Your product probably also has a Category property that refers to the category instance. This is the one you need to data bind, not the CategoryID.  If you modify just the foreign key (CatID) when the Category property is already set then L2S will not know what to do.  They have to agree in value.  If you make your combo box show a list of Category objects and then assign the Category property on your Product everything will work out fine.

    Saturday, November 29, 2008 5:47 PM
    Moderator
  •  

    Ok, but Category property (association) consists of CatID and CatName, how would a combobox know which one to  show? If i databind manually in designer to Products - Category.CatID then it shows correctly, but then i get this error on submit:

     

    "Value of member 'CatID' of an object of type 'Category' changed. A member defining the identity of the object cannot be changed. Consider adding a new object with new identity and deleting the existing one instead."

     

    As ive said, i have done allot of reading still cannot find a right solution.

     

    Do you men i will HAVE TO databind manually all the time, and everytime my Product Entity wants to change its Owner Entity (Product/Category) i will have to delete the Product and recreate it?

     

    This is all so confusing.

    Sunday, November 30, 2008 11:30 AM
  • Bind the comboBox to the Category property via the SelectedItem.  Fill the combo box with Category instances.  You'll also have to override ToString on Category to return the text you want to show in the combo box.
    Monday, December 1, 2008 5:09 PM
    Moderator