我正在尝试创建一个POC应用程序来探索Castle.ActiveRecord但由于某种原因,我得到了#34;无法编译映射文档:(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)}
答案 0 :(得分: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活动记录配置中删除“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;"/>
检查您正在使用的MSSQL版本,因为您已设置MsSql2000Dialect