❔ Translate entity framwork code to SQL
I have some database change and I need to create a script to run it on the live database eventually.
I need to update a table but i have no idea how to do it in SQL so i made the code in c# to explain the logic, but it need to be in SQL for the script
13 Replies
This is the c# code:
This is the actual database structure simplified:
This would be the new database structure (again, simplified):
This is my script:
use ef to generate the SQL then
Since it is in a for loop, it would generate a sql code for each iteration wouldnt it?
yes
which is why you should query that without the loop
look up navigation properties and what joins are
This is code written in notepad, so very untested:
UPDATE ClientAppointments ca
SET TherapyPayInformationId =
(SELECT TOP 1 Id FROM TherapistPayInformation tpi
WHERE tpi.TherapistId = ca.TherapistId AND
tpi.ChangedDate < ca.ClientBillGenerationDate
ORDER BY tpi.ChangedDate DESC)
Thanks I will try it tomorrow!
Oof ouch owie that's some garbage EF code
Just FYI
I dont know better :S Gotta take a course on udemy to learn how to do it without using a loop
.Include()
Or better yet, .Select()
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.