This is a migrated thread and some comments may be shown as answers.

Maybe beginner question for 1-to-many queries

2 Answers 23 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ueli
Top achievements
Rank 2
Ueli asked on 04 Aug 2014, 07:17 PM
Hi,

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)
addresults.substance_id=subID.id_substances

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

Ueli

2 Answers, 1 is accepted

Sort by
0
Accepted
Boyan
Telerik team
answered on 06 Aug 2014, 06:10 PM
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. 

Regards,
Boyan
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Ueli
Top achievements
Rank 2
answered on 21 Aug 2014, 07:30 PM
Thanks for the tip. Actually my code worked as well but I noticed now that I had a wrong condition in a previous step.

Ueli
Tags
LINQ (LINQ specific questions)
Asked by
Ueli
Top achievements
Rank 2
Answers by
Boyan
Telerik team
Ueli
Top achievements
Rank 2
Share this question
or