Here is a simple subtraction with 2 integers. The SQL Statement works but the value doesn't update in the database. Here is the code below.
var rest = antrag.Urlauber.RestUrlaub - antrag.Tage;
var sqlcom = "UPDATE Person SET RestUrlaub = " + rest +" WHERE ID = " + id +"";
db.Database.ExecuteSqlCommand(sqlcom);
Anyone got an idea what the problem might be?
The query looks okay.
The issue can be with your "id"
Execute the query manually on your database and see if it updates. If the id is wrong then the query returns success without updating anything.
Suggestion - Usage of queries like this are prone to very big security loopholes like SQL injection. Consider using ORMs like Entity Framework or pass query parameters as ADO parameters rather that stitching directly in query.
Optionally: if you didn't prefer to use big ORMs like Entity Framework, I've created a simple dapper based library that you can consider to run your queries in 1 or 2 lines safely.
How to use: https://github.com/sangeethnandakumar/Express-Data-Library
Installation
Install-Package Twileloop.ExpressData -Version 1.0.0
Running a parameterized query safely to prevent SQL Injection
var sql2 = $ "SELECT * FROM tblUser WHERE Id=@id AND Fname=@fname";
result = SqlHelper.QuerySafe<TblUser>(sql2,
new
{
id = 1,
fname = "Sangeeth"
},
_connectionString);