C
C#2y ago
N0mlss

❔ 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
N0mlss
N0mlssOP2y ago
This is the c# code:
N0mlss
N0mlssOP2y ago
This is the actual database structure simplified:
N0mlss
N0mlssOP2y ago
This would be the new database structure (again, simplified):
N0mlss
N0mlssOP2y ago
This is my script:
Patrick
Patrick2y ago
use ef to generate the SQL then
N0mlss
N0mlssOP2y ago
Since it is in a for loop, it would generate a sql code for each iteration wouldnt it?
Anton
Anton2y ago
yes which is why you should query that without the loop look up navigation properties and what joins are
Axiss
Axiss2y ago
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)
N0mlss
N0mlssOP2y ago
Thanks I will try it tomorrow!
Angius
Angius2y ago
Oof ouch owie that's some garbage EF code Just FYI
N0mlss
N0mlssOP2y ago
I dont know better :S Gotta take a course on udemy to learn how to do it without using a loop
Angius
Angius2y ago
.Include() Or better yet, .Select()
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server