DateTime.UtcNow returns 0001-01-01T00:00:00
Hello guys I have BaseEntity and it has createdAt,modifiedAt values in it
So the problem is when i run this code it returns
But i want the values to be same when it's first created
28 Replies
Seems like a faulty conclusion you are making there.
Have you verified that the state truly is
Added
or Modified
in the case above?Looks like
DateTime.UtcNow
isn't the issue, since it has already assigned its value to CreatedAt
, but like obiega already mentioned, are you sure the state is trully Added
or Modified
? Because it's possible that the state is actually none of the two, and CreatedAt
has been given a value somewhere else outside of this SaveChangesAsync
function
Can you show the piece of code where the SaveChangesAsync
function is being called from?
@baristanerSure
So this has no issues
But the problem is here
I've never called the function in here tho
well uh
thats you activating a stored procedure via raw SQL
Okay so, have you verified whether the state is either Added / Modified?
the changetracker is not aware of any changes done via rawsql or via stored procedures
yeah it's a stored procedure, why should that even assign a value to modifiedAt from the code?
unless you have a piece of SQL code that assigns a value to modifiedAt, but let's keep that aside
Your question is about the UpdateCode function, updating values, and not seeing the expected value back in the database
Stored Procedure does nothing to that entity it inserts values into Codes table And the BaseTable is basically just the default values like Id,createdAt,modifiedAt
createdAt is correct but modifiedAt is not
it creates the timestamp perfectly except one thing.
So when i run this procedure it will automatically create these fields
have you debugged your code and checked the code step-by-step inside the savechangesasync?
have you confirmed that the state is either added or modified?
Trying that rn
brb
yes plz
I just ran UpdateCode()
okay, so it does assign a value to it
and in the database?
Yep
But not when i run the stored Procedure
well then i need to see your stored procedure sql statement
Not the modifiedAt i mean
if you could copy your sql statement here
Sure mate
ok lemme check
My entities
Code.cs
Base.cs
INSERT INTO dbo.Codes (CODE, ExpirationDate,createdAt,IsActive)
VALUES (@RandomString, @ExpirationDate,GETDATE(),1);
you dont insert anything into the ModifiedAt column?
I do see createdAt, but not modifiedAt
oh yeah i totally forget about it
ALTER PROCEDURE [dbo].[GenerateCodes]
@NumOfCodes INT = 50,
@CharacterSet NVARCHAR(50) = '13456789ACDEFHKLMNPQRTVWXYZ',
@ExpirationMonths INT = 3,
@ExpirationDate DATE = NULL /* TIME FORMAT ; YYYY-MM-DD */
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Counter INT = 1;
BEGIN TRY
BEGIN TRANSACTION;
WHILE @Counter <= @NumOfCodes
BEGIN
DECLARE @RandomString VARCHAR(10);
SET @ExpirationDate = DATEADD(MONTH, 3, GETDATE());
SET @RandomString = '';
DECLARE @InnerCounter INT = 1;
WHILE @InnerCounter <= 10
BEGIN
SET @RandomString = @RandomString + SUBSTRING(@CharacterSet, (ABS(CHECKSUM(NEWID())) % LEN(@CharacterSet) + 1), 1);
SET @InnerCounter = @InnerCounter + 1;
END;
SET @UtcNow = GETUTCDATE();
INSERT INTO dbo.Codes (CODE, ExpirationDate,CreatedAt,ModifiedAt,IsActive)
VALUES (@RandomString, @ExpirationDate,@UtcNow,@UtcNow,1);
SET @Counter = @Counter + 1;
END;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
PRINT 'An error occurred. Transaction rolled back.';
PRINT ERROR_MESSAGE();
END CATCH;
END;
something like thisWhat if i call the function _context.SaveChangesAsync();
and remove those values in stored procedure?
you can either do the code in C#, or you can let the stored procedure do it
but the savechangesasync will handle the createdAt and modifiedAt
EF (where the context and
SaveChanges
comes from) isn't aware of any changes done by the stored procedure
you either do this with EF, or you do it with the stored procedure. Not both.if that's what @baristaner is asking, then indeed EF isn't aware of the changes done by the stored procedure.
Okay gotcha thanks guys
I'm trying a different approach rn