Maybe beginner question for 1-to-many queries

Thread is closed for posting
3 posts, 1 answers
  1. Ueli
    Ueli avatar
    5 posts
    Member since:
    Dec 2013

    Posted 04 Aug 2014 Link to this post


    I'm a bit stuck with a query which worked with entity framework but here in OpenAccess it doesn't do what I want. Maybe because I'm still a beginner. What I need is to get an ID of an existing entry to be inserted. The query is as follows:

    addResults.Substance_id = mysqlhplc.Substances.Where(Function(p) p.Substance1 = subName).Select(Function(s) s.Id_substances).FirstOrDefault()

    This always ends with addResults.Substance_id with a value of 0 although it should be 1. subName is the name of the entry and the variable is correct.

    There are two tables, results and substances. They are connected by a 1(substances) to many (results) relationship. I tried the same with the query below with the same result:

    Dim subId as Substance=(From s in Substances
    where s.substance1=subName
    select s)

    In the documentation I only found a solution to directly add a new item to the related table.

  2. Answer
    Boyan avatar
    100 posts

    Posted 06 Aug 2014 Link to this post

    Hi Ueli,

    In order to obtain the Id of an object you are about to insert you would need to flush the changes to the database. You could do that using the .FlushChanges() method available through a Telerik Data Access context. Once you call .FlushChanges()​ the auto-incremented ids of objects added to the context will be populated. At this point your queries should return the correct result. You could read more on how to handle transactions through a Telerik Data Access context in this documentation article.

    Additionally, if you would like to use this functionality to manage associations between your tables you have to know that Telerik Data Access could manage them automatically for you. To take advantage of this feature you need to enable the .IsManaged property of the collection navigational property. You could do that through the Visual Designer by selecting the navigational property and pressing F4 then from the properties windows that should have shown, setting the Is Managed to true. For more detailed explanation on this feature please refer to this documentation article.

    If this is actually not what you need, could you please provide a bit more information on your implementation scenario? Also please provide the entire code snippet that demonstrates this behavior.

    Do not hesitate to contact us with any further questions. 

    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. Ueli
    Ueli avatar
    5 posts
    Member since:
    Dec 2013

    Posted 21 Aug 2014 in reply to Boyan Link to this post

    Thanks for the tip. Actually my code worked as well but I noticed now that I had a wrong condition in a previous step.

Back to Top