Search code examples
c#sql-serverwinformsnhibernatecastle-activerecord

Castle ActiveRecord not connecting to the database


I recently got a work assignment to learn everything about this framework, but I can't even get it started. I read all the blogs and documentations about this particular initialization part but all of them are at least 3-4 years old and there is no updated information about it. This is the official documentation on Castle ActiveRecord. I changed it a bit since the InPlaceConfigurationSource no longer has an overload that uses the HashTable and I think this is the problem with it, since i get this exception.

    public Form1()
    {
        InitializeComponent();

        InPlaceConfigurationSource source = new InPlaceConfigurationSource();

        IDictionary<string,string> properties = new Dictionary<string, string>();

        properties.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
        properties.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2008Dialect");
        properties.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
        properties.Add("hibernate.connection.connection_string", "Server=.\\SQLEXPRESS;Initial Catalog=TestDB; Trusted_Connection=true;");

        source.Add(typeof(ActiveRecordBase), properties);

        ActiveRecordStarter.Initialize(source, typeof(Blog));
    }

The type initializer for 'Castle.ActiveRecord.ActiveRecordStarter' threw an exception.

"Could not load type 'Iesi.Collections.Generic.HashedSet'1' from assembly 'Iesi.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'.":"Iesi.Collections.Generic.HashedSet`1"


Solution

  • I managed to solve this and I will post the answer in case anyone else gets stuck in this. I solved it by deleting all the added references and looking for older dll's in another project and manually referencing them in the solution explorer. The NuGet manager returns only newer versions which are not compatible with each other (ActiveRecord, NHibernate, log4net, etc.) or don't have all the old classes defined anymore (i.e. the exception in this post)