I have a C# application that receives JSON data and writes a model back to Universe. In a number of models, I need to separate DATE and TIME fields and write them back individually when the model is created.
NET Framework V4.6.1, U2.Data V2.2.2 NuGet package, U2NDK V2.2.2, Universe V11.2.5
I created the model in Universe using RocketU2:
@ID TYPE LOC CONV ... DATA TYPE
Id D 0 CHAR(30)
AdjustmentDate D 1 D4- DATE
AdjustmentTime D 2 MTS TIME
My first reaction was to write this in the controller:
AdjustmentTime = DateTime.Now.TimeOfDay;
And this in the C# app models:
public TimeSpan AdjustmentTime { get; set; }
This resulted in the error: "There is no store type corresponding to the EDM type 'Edm.Time' of primitive type 'Time'."
There were a number of questions regarding how to post time separately to a database. So I've tried those, and here is the result:
ProductAdjustmentController:
[Route("")]
[HttpPost]
public HttpResponseMessage PostProductAdjustment([FromBody] ProductAdjustmentCreate productAdjustmentCreate)
{
ResponseCollectionMember _response = new ResponseCollectionMember();
try
{
var id = productAdjustmentCreate.ProductId + "*" + productAdjustmentCreate.AdjustmentDate.ToString() + "*" + productAdjustmentCreate.AdjustmentTime.ToString();
var productAdjustmentEntity = new ProductAdjustmentEntity()
{
Id = id,
AdjustmentDate = DateTime.Now.Date,
AdjustmentTime = DateTime.Now.TimeOfDay.Ticks,
};
_context.ProductAdjustments.Add(productAdjustmentEntity);
}
ProductAdjustmentEntity model:
public class ProductAdjustmentEntity : FileBase<string>
{
public string ProductId { get; set; }
public DateTime? AdjustmentDate { get; set; }
public long AdjustmentTime { get; set; }
[NotMapped]
public TimeSpan Time
{
get { return TimeSpan.FromTicks(AdjustmentTime); }
set { AdjustmentTime = value.Ticks; }
}
}
ProductAdjustmentCreate model:
public class ProductAdjustmentCreate
{
[Required]
public string ProductId { get; set; }
public DateTime? AdjustmentDate { get; set; }
public long AdjustmentTime { get; set; }
[NotMapped]
public TimeSpan Time
{
get { return TimeSpan.FromTicks(AdjustmentTime); }
set { AdjustmentTime = value.Ticks; }
}
}
I get the same unsupported primitive type error for a different type: "There is no store type corresponding to the EDM type 'Edm.Int64' of primitive type 'Int64'."
I dug down into the U2.Data package and found that U2.Data.ClientTypes.Int64 and Time DO exist. The last thing I tried was creating a method that would cast my model creation values into the U2-accepted value:
public static U2Int64 ConvertToU2Int64(long time)
{
U2Int64 u2Time = time;
return u2Time;
}
But all this does is return null.
I just want this to work so I can test posting the model to Universe.
My questions for working through this process: Is there a simple way to do this? How would I get the primitive types to be recognized? How would I write a method to convert them that I could use throughout the app?
Generically, you are trying to find a datatype for a column (well, in RDBMS terms) that plays nice with the dotnet datatype.
Here is a MySql .. AS AN EXAMPLE.. of this kind of mapping.
https://www.devart.com/dotconnect/mysql/docs/DataTypeMapping.html
After you find the magic datatype..you have to tell EF to use it (instead of relying on defaults)
https://learn.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations%2Cfluent-api%2Cwithout-nrt#column-data-types
in the above, the attribute
is overwriting any default......
I don't have any Rocket DB installed, so I cannot try anything concretely.
But maybe
or
?
If you prefer "fluent" for your ORM mappings, it would be "HasColumnType".
https://www.learnentityframeworkcore.com/configuration/fluent-api/hascolumntype-method