OrientDB-NET.binary icon indicating copy to clipboard operation
OrientDB-NET.binary copied to clipboard

Problem inserting object with date time fields

Open degustator opened this issue 10 years ago • 4 comments

Seems there is error in mapping in class Orient.Client.Mapping.DateTimeFieldMapping if object contains DateTime fields

Code fragments:

.... class File { public String fileName { get; set; } public DateTime created { get; set; } } ... using (ODatabase database = new ODatabase(DBNAME)) {

                database.Create.Class<File>().Extends<OVertex>().CreateProperties().Run();
            }

When inserting exception appears:

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Unable to cast object of type 'System.DateTime' to type 'System.String'.
  Source=Orient.Client
  StackTrace:
       at Orient.Client.ODocument.GetField[T](String fieldPath) in c:\Temp\OrientDB\Orient.Client\API\Types\ODocument.cs:line 169
       at Orient.Client.Mapping.DateTimeFieldMapping`1.MapToNamedField(ODocument document, TTarget typedObject) in c:\Temp\OrientDB\Orient.Client\Mapping\DateTimeFieldMapping.cs:line 20
       at Orient.Client.Mapping.NamedFieldMapping`1.MapToObject(ODocument document, TTarget typedObject) in c:\Temp\OrientDB\Orient.Client\Mapping\FieldMapping.cs:line 65
       at Orient.Client.Mapping.FieldMapping`1.MapToObject(ODocument document, Object typedObject) in c:\Temp\OrientDB\Orient.Client\Mapping\FieldMapping.cs:line 46
       at Orient.Client.Mapping.TypeMapper`1.ToObject(ODocument document, T typedObject) in c:\Temp\OrientDB\Orient.Client\Mapping\TypeMapper.cs:line 157
       at Orient.Client.ODocument.ToObject[T](T genericObject, String path) in c:\Temp\OrientDB\Orient.Client\API\Types\ODocument.cs:line 326
       at Orient.Client.ODocument.To[T]() in c:\Temp\OrientDB\Orient.Client\API\Types\ODocument.cs:line 301
       at Orient.Client.ORecordCreateVertex.Run[T]() in c:\Temp\OrientDB\Orient.Client\API\Query\ORecordCreateVertex.cs:line 129
       at OrientDBTest.Program.Main(String[] args) in c:\TFS\NGP\NTT\Main\Auxillary\OrientDBTest\Program.cs:line 71
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

At this moment changing class to following code helps

namespace Orient.Client.Mapping
{
    internal class DateTimeFieldMapping<TTarget> : BasicNamedFieldMapping<TTarget>
    {
        public DateTimeFieldMapping(PropertyInfo propertyInfo, string fieldPath)
            : base(propertyInfo, fieldPath)
        {

        }

        protected override void MapToNamedField(ODocument document, TTarget typedObject)
        {
            DateTime dateTime = document.GetField<DateTime>(_fieldPath);

            SetPropertyValue(typedObject, dateTime);

        }
    }
}

degustator avatar Feb 09 '15 15:02 degustator

I've created a PR for a test case for this. I cannot get it to fail on OrientDB 1.7.9, 2.0.1 or 2.0.2. Can you please try this test case on your system or update it to show it failing?

poindextrose avatar Feb 09 '15 20:02 poindextrose

The problem in my case that ORIENTDB DATETIMEFORMAT is missing Milliseconds. To fix issues with DateTime fields driver need to know DATETIMEFORMAT and TIMEZONE in the DataBase Configuration. I don't know if possible at all to query such information.

I will merge you PR but this issue stay opened.

GoorMoon avatar Feb 13 '15 11:02 GoorMoon

Open issue to orientdb repository https://github.com/orientechnologies/orientdb/issues/3583

GoorMoon avatar Feb 13 '15 12:02 GoorMoon

please share me the code for oreintdb 2.2.33 database connection through c#.net

user33 avatar Sep 18 '18 03:09 user33