DateTime syncing issue in Azure Mobile Services - c#

The problem is I have 2 columns CreatedAt and UpdatedAt in text data type in local sqlite and on server these two columns are of datetimeoffset data type.
When I insert/update any record using MobileServiceClient of azure, everything gets into database except these two column resulting in NULL values.
Similarly, if I update these column using SQLite connection, it updates records just fine. However, azure does not take these two columns' values on server when I sync.
Things I have tried:
Inserting every possible format of date time in these columns, which I thought is the problem.
Changing column's data type to match the data type on server but MobileServiceClient inserts default date and time which is 01/01/0001
I'm having trouble understanding how this service handles the data locally and on server.
Any help?

When creating the table on the server, it used to set those two columns up to get values automatically, not populated from the client.
The DDL for the columns looks like this:
[createdAt] datetimeoffset(3) NOT NULL DEFAULT (CONVERT([datetimeoffset](3),sysutcdatetime(),(0))),
[updatedAt] datetimeoffset(3) NULL,
and there is a trigger for updatedAt:
CREATE TRIGGER [DbName].[TR_TableName_InsertUpdateDelete] ON [DbName].[TableName]
WITH EXECUTE AS CALLER
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
UPDATE [DbName].[TableName] SET [DbName].[TableName].[updatedAt] = CONVERT (DATETIMEOFFSET(3), SYSUTCDATETIME())
FROM INSERTED
WHERE INSERTED.id = [DbName].[TableName].[id]
END
GO

Related

Continuosly get stream of each insertions from SQL Server table

I want to monitor insertions in an SQL Server table. Like two columns UserID and Activity from a table (userData) so that as soon as the insertion happens to this table, I get the values that were inserted and passed to C#.
I want to use each insertion for comparison like comparing each insertion with some value and take actions upon them.
PS. I know how to get data from SQL Server and insert data to SQL Server table using C#. But don't know how to achieve it on a real-time basis to take decisions upon them.
You can use SqlDependency class and use it's OnChange event. Go through the linked MSDN document to see an example on how.
dependency.OnChange+=new
OnChangeEventHandler(OnDependencyChange);
Create an Insert Trigger
CREATE TRIGGER [dbo].[userDataInsert]
ON [dbo].[userData]
FOR INSERT
AS
BEGIN
SET NOCOUNT ON
select *
from inserted
-- do whatever you need with inserted
END
Use can also look at SqlTableDependency. It is a c# component raising events when a record is changes. You can find more detail at: https://github.com/christiandelbianco/monitor-table-change-with-sqltabledependency
SqlTableDependency is a high-level C# component used to audit, monitor and receive notifications on SQL Server's record table changes. For any record table change, as insert, update or delete operation, a notification containing values for the record changed is delivered to SqlTableDependency. This notification contains insert, update or delete record values.
This table record tracking change system has the advantage to avoid a select to retrieve updated table record, because the updated table values record is delivered by notification.

getting "DateTime" of the last change in rows in a table in SQL Server?

Is there any way to find "DateTime" of the last change in rows in a table in SQL Server?
The changes (Insert / Update) are submitted by another windows application
And all I have in this table is insert_Date and there is no update_Date (and I can't add any columns or use triggers)
I've tried some queries, but all I got was the number of "User Updates" in a table, not the IDs of modified rows!
I want to get rows which are modified or inserted after a specific DateTime
If the information isn't stored in the table (or in another one by using a trigger for example) then it's impossible to track which rows were inserted after a determined datetime. You might find the time the last operation was executed at a table/index level (by querying sys.dm_db_index_usage_stats) but not at a record level.
You can't find data that doesn't exist!

Azure Offline Sync Updated At pulls whole data during the Incremental Sync

I have a table controller, which has one way sync (Only get since there is no any update from mobile). So the very first time when I do the PullAsync it has pulled whole data since my Update At filed has the same timestamp for all the records. Now Second time I just wanted to do the incremental Pull (Note I do pass a name value to PullAsync to enable incremental sync).
But the problem is, it retrieves whole data again since there is no updated time stamp in Updated At (Because there is no update method in the Table Controller since it is one-way sync-Azure to mobile). How can I omit pulling data again if there is no update while all the Update At values are same time stamp? The issue is same as mentioned in below link. Do we still need to do one record update in order to fix this?
https://social.msdn.microsoft.com/Forums/office/en-US/ff002e85-1313-449f-89eb-cd45c1a4846c/mobile-services-sync-pullasync-and-updatedat?forum=azuremobile
Thanks in Advance
How can I omit pulling data again if there is no update while all the Update At values are same time stamp? The issue is same as mentioned in below link. Do we still need to do one record update in order to fix this?
I have checked that this issue still exists. AFAIK, the PullAsync operation would pull 50 records (MaxPageSize by default is 50), then begin a transaction to insert / update the retrieved entities, then update the local __config table with the latest updatedAt timestamp, then try to retrieve next page records and execute the above processing again.
Note: The format of id column looks like this deltaToken|{table-name}|{query-id}.
The pull operation request would look like:
https://<app-name>.azurewebsites.net/tables/<table-name>?$filter=(updatedAt ge datetimeoffset'2018-02-21T08:58:45.446Z')&$orderby=updatedAt&$skip=0&$top=50&__includeDeleted=true
I assume that you could query the local __config table to retrieve the latest updatedAt value or query the related local table to retrieve the latest updatedAt value, then add a small time interval to the current updatedAt and explicitly specify the filter on UpdatedAt property with the previous updatedAt when using the incremental pull operation.

Entity Framework 4 and SYSUTCDATETIME ()

I have a Table in my DataBase (MS SQL 2008) with a default value for a column SYSUTCDATETIME ().
The Idea is to automatically add Date and Time as soon as a new record is Created.
I create my Conceptual Model using EF4, and I have created an ASP.PAGE with a DetailsView Control in INSERT MODE.
My problems:
When I create a new Record. EF is not able to insert the actual Date and Time value but it inserts instead this value 0001-01-01 00:00:00.00.
I suppose the EF is not able to use SYSUTCDATETIME () defined in my DataBase
Any idea how to solve it? Thanks
Here my SQL script
CREATE TABLE dbo.CmsAdvertisers
(
AdvertiserId int NOT NULL IDENTITY
CONSTRAINT PK_CmsAdvertisers_AdvertiserId PRIMARY KEY,
DateCreated dateTime2(2) NOT NULL
CONSTRAINT DF_CmsAdvertisers_DateCreated DEFAULT sysutcdatetime (),
ReferenceAdvertiser varchar(64) NOT NULL,
NoteInternal nvarchar(256) NOT NULL
CONSTRAINT DF_CmsAdvertisers_NoteInternal DEFAULT ''
);
My Temporary solution:
Please guys help me on this
e.Values["DateCreated"] = DateTime.UtcNow;
More info here:
http://msdn.microsoft.com/en-us/library/bb387157.aspx
How to use the default Entity Framework and default date values
http://msdn.microsoft.com/en-us/library/dd296755.aspx
The problem is that EF doesn't know about that default value so it sends .NET default value to database. It is possible to force EF to use default value from DB but you must manually modify EDMX file (XML not designer). You must find column definition in SSDL part of EDMX and add StoreGeneratedPattern="Computed" attribute. You also must the same in CSDL part (this can be done in designer).
This setting will have several problems. First of all each update from database will delete your changes in SSDL. Also each insert and update will trigger select which will requery value from this column (because EF thinks that it is computed every time).
In your ASP .NET page when you create the record are you actually adding a value for the DateCreated value. I see that you are doing that now, which is what you needed to be doing in the first place.
So I would guess what was happening earlier is that you were creating your new record but not setting a value for the DateCreated Field. But since a DateTime has a default value of 0001-01-01 00:00:00.00 is why you were getting that behavior.
The default value would only work for the case where you insert a row but provide no indication of what to do with the DateCreated field. But the way that EF works it is always going to provide a value for this (unless you write your own insert query to override the generated ones).
You will also be potentially changing the value any time that you update the record as EF won't know what to do with it.

How do you get a NOW() value in C# using the MySQL Connector?

Is it possible in an easy way to get the NOW() timestamp from an UPDATE query? I'm trying to save the "lastupdated" value in the local cache, or is there in any way possible to get the exact MySQL server time which the update query was executed?
Best Regards; Görgen
To my knowledge, MySQL doesn't have functionality like Oracle's RETURNING or SQL Server's OUTPUT clause to be able to save a query by returning values from INSERT/UPDATE statements. So that means two statements minimum...
is there in any way possible to get the exact MySQL server time which the update query was executed?
The best I can think of is to define an audit column (they were standard approach at my previous work) for logging the timestamp when the record was updated. In MySQL, you can default the value so on update it is set to the timestamp value at that time:
ALTER TABLE your_table
ADD COLUMN update_timestamp TIMESTAMP NOT NULL DEFAULT ON UPDATE CURRENT_TIMESTAMP
...then this gives you a specific column value to query.
The usual way is to set a LastUpdated field in the database, either in the stored procedure or in a trigger. Then you can read it back.

Categories