views:

280

answers:

2

As the result of yesterday's discussion , I have decided to use Castle ActiveRecord for my ORM task. I have added attributes to the class according to the starting guide. After fixing some obvious errors, I was greeted with this:

*Could not find configuration for CLASS_XXX or its root type Castle.ActiveRecord.ActiveRecordBase this is usually an indication that the configuration has not been setup correctly*

Where CLASS_XXX in the error message is one of my c# class name. What is interesting is that CLASS_XXX is different each time I run the code. I have turned on log4net and my log.txt captured nothing. So, I am guessing the error occurred before the process reaches NHibernate.

Following is my CSharp code

        log4net.Config.XmlConfigurator.Configure();

        InPlaceConfigurationSource source = 
                new InPlaceConfigurationSource();


        Assembly asm = Assembly.Load("DomainModel.Entities");

        ActiveRecordStarter.Initialize(asm,source);
        ActiveRecordStarter.CreateSchema();

Any suggestion for finding the real cause of this problem?

+1  A: 

Either you're missing the configuration for the InPlaceConfigurationSource (sample) or you need to use a different initialization method.

Mauricio Scheffer
The link you provided to "sample" was actually incorrect. It points to the "InvalidRootTypeTestCase", which is supposed to throw Exception. However, this link gives me a GREAT start point--Tests for the source. This is the first time I actually use tests as the primary documentation. I downloaded all the source and read the tests, and it turned out that it was indeed an initialization problem. All I need is a simple var source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource; And things started getting together. Thank for the direction.
Wei Ma
the link is not incorrect... it shows how to programmatically configure a InPlaceConfigurationSource
Mauricio Scheffer
+1  A: 

This is not likely correct:

InPlaceConfigurationSource source = new InPlaceConfigurationSource();

You need to either do something like this:

string connectionString =
  System.Configuration.ConfigurationManager.
    ConnectionStrings["Northwind"].ToString();
InPlaceConfigurationSource source =
  InPlaceConfigurationSource.Build(
    DatabaseType.MSSQLServer2005, connectionString
  );

Or something like this:

string connectionString =
    System.Configuration.ConfigurationManager.
      ConnectionStrings["Northwind"].ToString();
IDictionary<string, string> properties =
  new System.Collections.Generic.Dictionary<string, string>();
properties.Add("dialect", "NHibernate.Dialect.MsSql2005Dialect");
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("connection.provider",
  "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", connectionString);
properties.Add("proxyfactory.factory_class",
  "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

InPlaceConfigurationSource source = new InPlaceConfigurationSource();
source.Add(typeof(ActiveRecordBase), (IDictionary<string, string>)properties);
Michael Maddox
Your point is right. It's the initialization for source that caused the problems. However, since it was Mauricio Scheffer's link took me to the right place for finding the problem. I had to mark his advise as answer. Thanks for your great help, though.
Wei Ma