【问题标题】:Castle.ActiveRecord - Could not compile the mapping document: (string)Castle.ActiveRecord - 无法编译映射文档:(字符串)
【发布时间】:2016-07-29 13:37:36
【问题描述】:

我正在尝试创建一个 POC 应用程序来探索 Castle.ActiveRecord,但由于某种原因,我收到“无法编译映射文档:(字符串)”错误消息。这是一个非常基本的代码。我检查了 Castle.ActiveRecord 的现有文档,但我无法弄清楚这个问题的原因。当然,它看起来像是某种配置故障。

程序.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);
        }
    }
}

学生.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)}

【问题讨论】:

    标签: c# nhibernate castle-activerecord


    【解决方案1】:

    在你的域定义中让你的属性“虚拟”,它看起来像这样:

    [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 活动记录配置中删除“休眠”命名空间

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

    检查您正在使用的 MSSQL 版本,因为您设置了 MsSql2000Dialect

    【讨论】:

    • 我做了您建议的更改,但没有运气。我仍然收到相同的错误消息。我还用错误消息的详细信息更新了我的问题。
    • 移除“hibernate”命名空间解决了这个问题。非常感谢您的快速解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2014-12-16
    • 2015-05-11
    • 2023-03-21
    相关资源
    最近更新 更多