Castle.ActiveRecord - 无法编译映射文档:(字符串)

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

我正在尝试创建一个 POC 应用程序来探索 Castle.ActiveRecord,但由于某种原因,我收到了 "Could not compile the mapping document: (string)" 错误消息。这是一个非常基本的代码。我检查了 Castle.ActiveRecord 的现有文档,但我无法弄清楚这个问题的原因。它看起来像是某种配置故障。

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 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)}

在您的域定义中创建属性 "virtual",它看起来像这样:

[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; }
}       

NHibernate 需要这样做才能进行延迟加载(Active Record 基于 NHibernate)

在此处查看更多内容:nhibernate and virtual class properties?

此外,如果它没有帮助,请考虑查找详细消息(通过提高日志级别),因为通常 Active Record 可以告诉您确切的错误所在

此外,尝试从您的 app.config 活动记录配置中删除 "hibernate" 命名空间

<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;"/>

检查您设置 MsSql2000Dialect 时使用的 MSSQL 版本