0

As brought forwarded from the previous question here.

I am using the DTO method to inner join data. Now is it possible to update data at my joined result? How the data will be updated back to the origin table where it should be possible be?

Requirement:

  • I am using Entity Framework, C#, ASP.NET Web API
  • I am using SQL Server

Currently I successfully joined employee and department table based on their ID. I joined employee and workingshifts based on their shift_id.

The inner join query was here:

from e in DSE.employees
join d in DSE.departments on e.department_id equals d.department_id
join ws in DSE.workingshifts on e.shift_id equals ws.shift_id

So the data are here:

[{"FirstName":"gg","LastName":"wp","Gender":"NoGender","Salary":8,"Department_id":1, 
   "Department_Name":"RND","Shift_id":"B","Duration":"afternoon"}]

Now I would like to update the information as follows:

FirstName: good game
LastName: well played
Gender: IGender
Salary: 8888
Shift_id: A
Duration: Morning

May I know that what code that should be done in my C# and Linq? Will it update back to my database tables? Moreover, I heard about this solution by using SQL stored procedure, you may show me this approach as well

Venkataraman R
  • 12,181
  • 2
  • 31
  • 58
Lawraoke
  • 540
  • 5
  • 25
  • It's not clear which entity data you are trying to update. Could you please elaborate? The LINQ you provided is just selecting data from joining two tables. – Md Hasan Ibrahim Mar 02 '20 at 04:03
  • Hi @MdHasanIbrahim after I successfully joined these tables. Then I would to perform an update at the joined result. However, back in my SQLserver, how did the data pass back to my origin table? – Lawraoke Mar 02 '20 at 06:32
  • So you want to update it using SQL server or Entity Framework? – JamesS Mar 02 '20 at 09:04
  • Hi @JamesS I would like to try both solution. For my understanding if I am not wrong, using Entity Framework is a better solution that using SQL server right? As if my project are using different database engine, then the solution for Entity Framework will be advantages. However, how to provide update by using Entity Framework? – Lawraoke Mar 02 '20 at 09:27
  • It really depends, personally I would always use stored procedures over entity framework as this can allow for (although it's not best practice as the code should be correct from the start) live changes without having to rebuild and redeploy code, saving time. However, for simply `SELECT` queries, it's probably best to use Entity Framework. Both have their advantages and disadvantages and it's mostly down to what the developer prefers at the time – JamesS Mar 02 '20 at 09:38
  • Hi @JamesS, is it possible for me to ask you by showing your way(for both Stored Procedure and Entities Framework)? – Lawraoke Mar 02 '20 at 09:59
  • @Lawraoke Yeah no problem, see my answer below. – JamesS Mar 02 '20 at 15:04

1 Answers1

3

Well, as per your comment. The SQL Server for updating a value/ values based on a join would be

UPDATE e
    SET e.FirstName = 'good game',
    e.LastName = 'well played'
FROM employees e
    INNER JOIN departments d ON d.department_id = e.department_id
    INNER JOIN workingshifts ws ON e.shift_id equals ws.shift_id
WHERE e.FirstName = 'gg'
    AND e.LastName = 'wp'

Without knowing the structure of the tables, I have simply used an INNER JOIN.

The Entity Framework to do this may be found:

var result = (from e in DSE.employees
              join d in DSE.departments on e.department_id equals d.department_id
              join ws in DSE.workingshifts on e.shift_id equals ws.shift_id
              where e.FirstName == "gg"
              && e.LastName == "wp"
              select e).FirstOrDefault()    // As it seems like we are just wanting employees however if you are expecting to return more than one employee back, you could use .ToList() instead of .FirstOrDefault()

if(result != null)
{
    result.FirstName = "Good Game";
    result.LastName = "Well Played";
    await context.SavechangesASync();   // If inside an async function, else just SaveChanges()
}

This is just if you are returning one employee. Like I said, if you are returning a list, you will need to incorporate the changes in the code above. With the above, you will also need to do a where on the result set where you want to the return the values you want

EDIT: Changed ' to " in the Entity Framework approach

JamesS
  • 2,167
  • 1
  • 11
  • 29
  • Hi @JamesS it seems that your approach was great for me. However, I tried for the Entity Framework solution of yours, *where e.FirstName == 'gg'*, it pop out error as **"Too Many Character on character literal"** So I tried to change from **'gg'** to "gg". Then after I run it I get a *null* result. So that means change to **"gg"** is unavailable too. Can you help me to sort this out? – Lawraoke Mar 03 '20 at 03:49
  • @Lawraoke Sorry, the EF approach should be `e.FirstName == "gg"` as it is a `string` not a `char`. The syntax is slightly different for SQL Just forgot to change the `'` to `"` – JamesS Mar 03 '20 at 08:57
  • Hi @JamesS after I changed to your approach, I get the result by `return result;` and I getting a **null** value. But I check back to my database the values still not changed yet (from **gg** to **good game**) Is it my join query wrong or something? – Lawraoke Mar 03 '20 at 09:40
  • @Lawraoke So you are getting the result back from the query, but when you update them and save, nothing is happening? You have enclosed all this in a `using` statement that opens the context to the database, haven't you? – JamesS Mar 03 '20 at 09:54
  • whoops sorry that was my bad, I put all these outside my `using()` However I tried to put inside the using, it still return me a *null* value. – Lawraoke Mar 03 '20 at 10:03
  • @Lawraoke If you try the equivalent SQL code, the one I have done for you, does that return data? – JamesS Mar 03 '20 at 10:05
  • Hi @JamesS I haven't yet implement the SQL code. Can you let me try it and get back to you? (It will take until tomorrow as I am off from work) – Lawraoke Mar 03 '20 at 11:03
  • Hi @JamesS just now I check back my database info, it had updated to **Good Game, Well Played** Well I didn't perform any update from yesterday until now. It was quite weird for you, I try another data set and apply for this situation – Lawraoke Mar 04 '20 at 01:37
  • 1
    Hi @JamesS after I check for the database data, turns out your coding worked. I need to manually refresh it and get the latest data. So thank you for solving my problem! :D – Lawraoke Mar 04 '20 at 01:49