none
Problem loading object from database. RRS feed

  • Question

  • Hi,

     

    I have a problem when loading an object from the database using Linq to Sql. My object model is as follows. I have an object of type Category which can have one or more objects of type Entry. An Entry has a reference of the Category object it is in. The problem is that when I load the Entry object the Category instance inside the Entry object remains NULL. I tried using the LoadWith as follows but still remains null.

     

    loadOptions.LoadWith<Blog.Entry>(entry => entry.Category);

     

    Is there a way how I can program Linq to Sql to load the Category object when the Entry object is loaded?

     

    I do not know if the above is enough clear. If not please do not hesitate to tell me and I will try to explain better.

     

    Thanks and Regards,

    Sim085

    Tuesday, August 12, 2008 9:50 PM

Answers

  • I seemed to have a link problem between the Entry and Category object. I changed the following to be like this and everything worked fine Smile       

    [Association(Storage = "category", OtherKey = "Id",
                ThisKey="CategoryId", IsForeignKey=true)]

    Thanks for your reply,
    Regards,
    Sim085
    Monday, August 18, 2008 8:17 PM

All replies

  • The Category object should be dynamically loaded when you access it from an Entry object unless it can not find a match.  LoadWith simply changes the behaviour so that it loads at the same time as the Entry object but neither should result in a null unless the DBML is configured wrong or the Entry object's foreign key to a Category object does not point to an actual Category object.

     

    [)amien
    Monday, August 18, 2008 7:58 PM
    Moderator
  • I seemed to have a link problem between the Entry and Category object. I changed the following to be like this and everything worked fine Smile       

    [Association(Storage = "category", OtherKey = "Id",
                ThisKey="CategoryId", IsForeignKey=true)]

    Thanks for your reply,
    Regards,
    Sim085
    Monday, August 18, 2008 8:17 PM