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

Error when retrieving an object with a field of type Dictionary<string, decimal>

Open dagimf opened this issue 8 years ago • 2 comments

I have a model that has a field of type Dictionary<string, decimal>, the decimal is necessary since its a financial payment data. I use to use double when i converted it to decimal it does not work. It says that it can not cast decimal in a generic list (paraphrased).

If you need i can submit the model and my test code. I tested this by creating a new db and creating the vertex class.

dagimf avatar Jul 21 '16 12:07 dagimf

@dagimf Sorry about the late reply. if you could include your code sample that would be great

GrayDelacluyse avatar Jul 28 '16 11:07 GrayDelacluyse

Here is the class i am using. Basically the exception happens when the ODocument is converted to c# equivalent model using the To method. The code i am using to run the program is below the class as well. I actually redesigned my db to not use dictionary, so i have found a work around.

The Model:


public class TestModel
    {
        public TestModel()
        {
            DecimalDictionaryTest = new Dictionary<string, decimal>();
            DecimalListTest = new List<decimal>();
        }
        public decimal  DecimalTest { get; set; }
        public Dictionary<string, decimal> DecimalDictionaryTest { get; set; }
        public List<decimal> DecimalListTest { get; set; }
    }

The test code:

static void Main(string[] args)
        {

            //drop any previously exsisting db
            try
            {
                OrientConnection.DropDatabase();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }


            //create the test database
            OrientConnection.CreateDatabase();

            //establish connection with the db
            OrientConnection.CreatePool();
            var database = new ODatabase(OrientConnection.GlobalDatabaseAlias);

            //create the schema
            database.Create.Class(typeof(TestModel).Name).Extends<OVertex>().Run();

            //model to save
            var testModel = new TestModel();
            testModel.DecimalTest = decimal.Parse("189.5");
            testModel.DecimalDictionaryTest.Add("key", decimal.Parse("189.5"));
            testModel.DecimalListTest.Add(decimal.Parse("189.5"));

            //save the test model
            database.Insert<TestModel>(testModel).Run();

            //get the saved data
            var selectQuery = new OSqlSelect()
                .Select()
                .From(typeof(TestModel).Name)
                .ToString();
            var testModelDocuments = database.Query(selectQuery);

            //convert it to the c# model
            var testModels = testModelDocuments.Select(document => document.To<TestModel>()).ToList();

        }

dagimf avatar Jul 29 '16 10:07 dagimf