Search code examples
c#nhibernatecastle-activerecord

Castle.ActiveRecord - Could not compile the mapping document: (string)


I am trying to create a POC application to explore Castle.ActiveRecord but for some reason, I am getting "Could not compile the mapping document: (string)" error message. It's a very basic code. I checked existing documentation of Castle.ActiveRecord but I couldn't able to figure about the reason of this issue. Surely its look like some kind of config glitch.

Program.cs

class Program
{
    static void Main(string[] args)
    {
        IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
        ActiveRecordStarter.Initialize(source, typeof(Student));

        var students = Student.FindAll();
        foreach (var student in students)
        {
            Console.WriteLine(student.Name);
        }
    }
}

Student.cs

[ActiveRecord("Student")]
public class Student : ActiveRecordBase<Student>
{
    [PrimaryKey]
    public int Id { get; set; }

    [Property]
    public string Name { get; set; }

    [Property]
    public string Grade { get; set; }
}       

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
  </configSections>

  <activerecord isWeb="true">
    <config>
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
      <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
      <add key="hibernate.connection.connection_string" value="Server=localhost;Database=Art_DEV;Trusted_Connection=True;"/>
    </config>
  </activerecord>

  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

Error I am getting is:

{"Error adding information from class ConsoleApplication1.Student to NHibernate. Check the inner exception for more information"}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233088
    HelpLink: null
    InnerException: {"Could not compile the mapping document: (string)"}
    Message: "Error adding information from class ConsoleApplication1.Student to NHibernate. Check the inner exception for more information"
    Source: "Castle.ActiveRecord"
    StackTrace: "   at Castle.ActiveRecord.ActiveRecordStarter.AddXmlString(Configuration config, String xml, ActiveRecordModel model)\r\n   at Castle.ActiveRecord.ActiveRecordStarter.AddXmlToNHibernateCfg(ISessionFactoryHolder holder, ActiveRecordModelCollection models)\r\n   at Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder holder, IConfigurationSource source, IEnumerable`1 types, Boolean ignoreProblematicTypes)\r\n   at Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource source, Type[] types)\r\n   at ConsoleApplication1.Program.Main(String[] args) in c:\\users\\user\\documents\\visual studio 2015\\Projects\\ConsoleApplication1\\ConsoleApplication1\\Program.cs:line 21"
    TargetSite: {Void AddXmlString(NHibernate.Cfg.Configuration, System.String, Castle.ActiveRecord.Framework.Internal.ActiveRecordModel)}

Solution

  • Make you properties "virtual" in your domain definition, it's going to look something like this:

    [ActiveRecord("Student")]
    public class Student : ActiveRecordBase<Student>
    {
        [PrimaryKey]
        public virtual int Id { get; set; }
    
        [Property]
        public virtual string Name { get; set; }
    
        [Property]
        public virtual string Grade { get; set; }
    }       
    

    That's required by NHibernate in order to do its lazy loading (Active Record is based on NHibernate)

    Look more here: nhibernate and virtual class properties?

    Also, if it doesn't help, consider looking for detailed message (by enhancing Log level) as usually Active Record can tell you what is exactly wrong

    Also, try removing "hibernate" namespace from you app.config active record configuration

    <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
    <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
    <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
    <add key="connection.connection_string" value="Server=localhost;Database=Art_DEV;Trusted_Connection=True;"/>
    

    Check MSSQL version you're using as you've set MsSql2000Dialect