简体   繁体   中英

Why can't I read a db4o file created by a Java app in a C# app?

I have a db4o database that was generate by a Java app and I'm trying to read it using a C# app.

However, when running the following line of code:

IObjectContainer db = Db4oEmbedded.OpenFile(@"..\..\..\Databases\people.db4o");

I get the following error:

Unable to cast object of type 'Db4objects.Db4o.Reflect.Generic.GenericObject' to type 'Db4objects.Db4o.Ext.Db4oDatabase'.

Any ideas? I know there are person objects that contain personId fields (along with others) in the DB. I'm using db4o version 8. I'm not sure what version was used to generate the database.

The entire program is:

using System;
using System.Collections.Generic;
using System.Linq;

using Db4objects.Db4o;
using Db4objects.Db4o.Config;

using MyCompany.Domain;

namespace MyCompany.Anonymizer
{
    internal class Program
    {
        // Private methods.

        private static IEmbeddedConfiguration ConfigureAlias()
        {
            IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();

            configuration.Common.AddAlias(new TypeAlias("com.theircompany.Person", "MyCompany.Domain.Person, MyCompany.Domain"));

            configuration.Common.Add(new JavaSupport());

            return configuration;
        }

        private static void Main(string[] args)
        {
            IObjectContainer db = Db4oEmbedded.OpenFile(@"..\..\..\Databases\people.db4o");

            try
            {
                IList<Person> result = db.Query<Person>();

                for (int i = 0; i < result.Count; i++)
                {
                    Person person = result[i];

                    Console.WriteLine(string.Format("Person ID: {0}", person.personId));
                }
            }
            finally
            {
                db.Close();
            }
        }
    }
}

The most common scenario in which this exception is thrown is when db4o fails to resolve the type of a stored object.

In your case, db4o is failing to read one of its internal objects which makes me believe you have not passed the configuration to the OpenFile() method (surely, the code you have posted is not calling ConfigureAlias() method);

Keep in mind that as of version 8.0 no further improvement will be done regarding cross platform support (you can read more details here ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM