Background
The field TimestampCreation is created as such in the DB:
[TimestampCreation] [datetime2](7) NOT NULL
If I add the field to a datatable like this:
table.Columns.Add(new DataColumn("TimestampCreation", typeof(DateTime)));
the value loses precision, because it's stored like so:
2016-12-07 11:38:39.4990000
When it should be
2016-12-07 11:38:39.4998426
One solution I tried
If I add the column like so:
table.Columns.Add(new DataColumn("TimestampCreation", typeof(string))); // Actually datetime2
The value is stored correctly to DB, but this feels dirty/hacky.
Usage in code
The value is set like this:
public static void AddToTable(this EntityDto source, DataTable table)
{
var row = table.NewRow();
...
...
row["TimestampLastModification"] = source.TimestampLastModification;
table.Rows.Add(row);
}
Where source.TimestampLastModification
is of type DateTime
:
public DateTime TimestampLastModification { get; set; }
Next the datatable is saved to storage like this:
Root.PluginManager().PersistentStorage(ParametersHelper.OverallSchema).Insert(ref table)
Question
What's the proper way to add a datetime2
field to a datacolumn and keep the expected precision?