Reflect changes from Trigger after Update - entity-framework

I have a SQL Server table with database Trigger (Update) where sometimes a value is changed depending on the Update values. I know how to reflect changes if there a coumputed columns like setting "ValueGeneratedOnAddOrUpdate":
builder.Entity<PDBIntern.Models.PDB.Pfuscherakt>()
.Property(p => p.Aktenzahl)
.ValueGeneratedOnAddOrUpdate();
but if I set ValueGeneratedOnAddOrUpdate the generated Update statement exclude this column. How to reflect changes from Trigger and also include this coumn in the result (Select after Update)
Or is there a possibility to define the columns which EF generates after update statement to reflect changes in the table?
regards Robert

Your ef code does not know what your trigger has done. How could it?
Try refreshing your context before any operation eg. update
await dbContext.Entry(PDBIntern.Models.PDB.Pfuscherakt).ReloadAsync();

Related

Trigger on any column change except specific one

I want to trigger on all column changes on a table except a specific one, but listing all the columns in the AFTER INSERT OR UPDATE OF clause is a problem because the table has many columns and the set of columns changes at times, making maintaining the trigger definition in sync very error prone. How can I do this in a way that I just specify the column to omit from the trigger? For the moment I have an event trigger that prints a warning to update the trigger when that table is altered, but that relies on the user noticing the warning and obviously won't help when creating a new db.

update trigger when updating table through SSMS

I have an update trigger that does what it needs to do: update a datefield triggered by an after update trigger.
However, when I edit the contents of a table through SSMS (right-click - Edit top 200 rows) it gives me a message that the row couldn't be committed. The error:
The data in row x was not committed.
Eror message: The row value(s) updated or deleted either do not make the row unique or they alter multiple ows(2rows).
If i disable the trigger an update through the interface works just fine.
The same upate through an TSQL update statement works fine when the trigger is activated (and trigger does what it needs to do)
What is causing this behaviour and how can I have ànd the trigger ànd update through the interface?
Lots of my updates are queries that contain single quotes so it is easier to paste them than write an update statement to update them)
The issue is that triggers apparently just don't play nice when using the edit functionality

sql update trigger to grab updated data and also select other row data

I am trying to find a way so that when a specific column gets updated on a table that an update trigger (or maybe something else) can then select the stop number column from the same row that the datetime was update on. I want to capture the stop number and the column data before/after the update into another table. I do ok with SQL but I'm no expert so I just can't think of how to accomplish this.
Is it possible?
Yes, it is. Have a read through this. Basically there are two virtual tables, deleted and inserted, that you can query in a trigger. Deleted contains the row that is being deleted, and inserted (you guessed it) the row being inserted.
"How does that help? I'm doing an update." Indeed but an update is effectively a delete followed by an insert, so in an after update trigger you can get at the old value in deleted.

would postgres really update page file when fields are all equals before and after update?

I am working with a little website crawler program. I use PostgresQL to store data and use such statement to update that,
INSERT INTO topic (......) VALUES (......)
ON CONFLICT DO UPDATE /* updagte all fields here */
The question is if all fields before upate and after update are really equals, would PostgresQL really update that?
Postgres (like nearly all other DBMS) will not check if the target values are different then the original ones. So the answer is: yes, it will update the row even if the values are different.
However, you can easily prevent the "empty" update in this case by including a where clause:
INSERT INTO topic (......)
VALUES (......)
ON CONFLICT (...)
DO UPDATE
set ... -- update all column
WHERE topic IS DISTINCT FROM excluded;
The where clause will prevent updating a row that is identical to the one that is being inserted. To make that work correctly your insert has to list all columns of the target tables. Otherwise the topic is distinct from excluded condition will always be true because the excluded row has fewer columns then the topic row and thus it id "distinct" from it.
Adding a check for modified values has been discussed multiple times on the mailing list and has always be discarded. The main reason being, that it doesn't make sense to have the overhead of checking for changes for every statement just to cope with a few badly written ones.

update the value of a column every time an update is made to the row

I am using SQL Server 2012. I need to track all the rows that were updates (as part of any action) made on a table.
I thought to add a new column (Column Name: "LastUpdated" of type Datetime.) to the table that I want to track. I would like to update the LastUpdated value every time an update is made to that row. Is there a specific way to acheive this task?
You could create a trigger. Like this:
CREATE TRIGGER dbo.Table1_Updated
ON dbo.Table1
FOR UPDATE /* Fire this trigger when a row is UPDATEd */
AS BEGIN
UPDATE dbo.Table1 SET dbo.Table1.LastUpdated = GETDATE()
FROM INSERTED
WHERE inserted.id=Table1.id
END
However, this trigger will not store WHAT was updated. Also keep in mind that if two people update it, you will only have the date of the most recent update (not all updates).
To keep a history on all changes, you might want to create an audit table (nearly identical structure to your existing table) and use a trigger to copy the pre-update data into the audit table.
This SO article talks about a solid approach: Using table auditing in order to have "snapshots" of table
You would want to look into creating a trigger to fire on update
Create TRIGGER Trigger_Name ON Table_Name
FOR update
AS
BEGIN
// update lastupdated column of updated record with current date and or time
END
Alliteratively you could just pass the new value for lastupdated when updating the other fields
We have implemented a system similar to the following:
Add a new column to your table LastUpdatedDate with a datatype of datetime
If needed, add another column with LastUpdatedBy with a datatype of varchar(100) or whatever length you need
This will store both the date/time and who performed the update.
Then when you update the row in the table, you will include those two columns in your update statement similar to this:
update yourtable
set yourCol = 'newValue',
LastUpdatedDate = getdate(),
LastUpdatedBy = 'yourUserIdentifier'