带接口的 ActiveRecord 崩溃
我在使用 ActiveRecord 和接口时遇到问题。当我在没有接口的情况下使用它时,一切都正常,但是当我将它与接口一起使用时,它有时起作用,有时不起作用,而且非常随机。如果它有效,它将继续工作,直到在我的项目中的某个地方进行一些更改并重新编译。然后,如果它停止工作,除非我再次进行一些不相关的更改并重新编译,否则它不会工作。 你可以在下面看到我的模型。您知道我该如何解决这个问题吗?
[ActiveRecord]
public class ContactInformation : ActiveRecordValidationBase<ContactInformation>
{
public ContactInformation()
{
Emails = new List<Email>();
}
[PrimaryKey]
public int Id { get; set; }
[HasMany(typeof(Email), Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, "type='1'")]
private IList<Email> Emails { get; set; }
[ActiveRecord("Details",
DiscriminatorColumn = "int",
DiscriminatorType = "type",
DiscriminatorValue = "1")]
public class Email : Detail<Email>
[ActiveRecord("Details",
DiscriminatorColumn = "int",
DiscriminatorType = "type",
DiscriminatorValue = "0")]
public class Detail<T> : ActiveRecordValidationBase<T>, IDetail where T : class
{
[PrimaryKey]
public int Id { get; set; }
[BelongsTo]
public ContactInformation ParentContactInformation { get; set; }
public interface IDetail
{
int Id { get; set; }
ContactInformation ParentContactInformation { get; set; }
}
以下例外:
SetUp : System.NullReferenceException : Object reference not set to an instance of an object.
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.WriteCompositeColumns(String[] columns)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.WriteCollection(ManyRelationCascadeEnum cascadeEnum, Type targetType, RelationType type, String name, String accessString, String table, String schema, Boolean lazy, Boolean inverse, String orderBy, String where, String sort, String columnKey, String[] compositeKeyColumnKeys, String element, Type elementType, String columnRef, String[] compositeKeyColumnRefs, IVisitable extraModel, String index, String indexType, CacheEnum cache, String cacheregion, NotFoundBehaviour notFoundBehaviour, FetchEnum fetch, Int32 batchSize, Type collectionType)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.VisitHasMany(HasManyModel model)
at Castle.ActiveRecord.Framework.Internal.HasManyModel.Accept(IVisitor visitor)
at Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable nodes)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.VisitModel(ActiveRecordModel model)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.CreateXml(ActiveRecordModel model)
at Castle.ActiveRecord.ActiveRecordStarter.AddXmlToNHibernateCfg(ISessionFactoryHolder holder, ActiveRecordModelCollection models)
at Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder holder, IConfigurationSource source, IEnumerable`1 types, Boolean ignoreProblematicTypes)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource source, Type[] types)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(Assembly[] assemblies, IConfigurationSource source, Type[] additionalTypes)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(Assembly assembly, IConfigurationSource source)
at tests.Framework.ModelTests.SetUpFixture() in ModelTests.cs: line 18
这是我的 SetUpFixture
[TestFixtureSetUp]
public void SetUpFixture()
{
if (ActiveRecordStarter.IsInitialized) return;
IConfigurationSource source = ActiveRecordSectionHandler.Instance;
ActiveRecordStarter.Initialize(Assembly.Load("Model"), source);
XmlConfigurator.Configure();
}
我检查了生成的文件,详细信息文件具有此文件名 Detail`1.hbm.xml 反引号和此内容:
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Contact.Detail`1, Model" table="Details" discriminator-value="0">
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<discriminator column="type" type="int" />
<property name="Owner" access="property" type="String">
<column name="Owner"/>
</property>
<property name="Location" access="property" type="String">
<column name="Location"/>
</property>
<property name="ContactDetail" access="property" type="String">
<column name="ContactDetail"/>
</property>
<many-to-one name="ParentContactInformation" access="property" class="Model.ContactInformation,Model" column="ParentContactInformation" lazy="proxy" />
<subclass name="Model.Contact.Email, Model" discriminator-value="3">
</subclass>
<subclass name="Model.Contact.Phone, Model" discriminator-value="1">
</subclass>
<subclass name="Model.Contact.Fax, Model" discriminator-value="2">
</subclass>
</class>
</hibernate-mapping>
I am having problems using ActiveRecord with Interfaces. When I use it without interfaces everything is working but when I use it with interfaces it works sometimes and not sometimes and it is very random. If it works it will continue work until a do some changes somewhere in my project and recompile. Then if it stops working it won't work until I do some unrelated changes again and recompile.
You can see my models below. Do you have any idea of how I can resolve this?
[ActiveRecord]
public class ContactInformation : ActiveRecordValidationBase<ContactInformation>
{
public ContactInformation()
{
Emails = new List<Email>();
}
[PrimaryKey]
public int Id { get; set; }
[HasMany(typeof(Email), Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, "type='1'")]
private IList<Email> Emails { get; set; }
[ActiveRecord("Details",
DiscriminatorColumn = "int",
DiscriminatorType = "type",
DiscriminatorValue = "1")]
public class Email : Detail<Email>
[ActiveRecord("Details",
DiscriminatorColumn = "int",
DiscriminatorType = "type",
DiscriminatorValue = "0")]
public class Detail<T> : ActiveRecordValidationBase<T>, IDetail where T : class
{
[PrimaryKey]
public int Id { get; set; }
[BelongsTo]
public ContactInformation ParentContactInformation { get; set; }
public interface IDetail
{
int Id { get; set; }
ContactInformation ParentContactInformation { get; set; }
}
The following exception:
SetUp : System.NullReferenceException : Object reference not set to an instance of an object.
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.WriteCompositeColumns(String[] columns)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.WriteCollection(ManyRelationCascadeEnum cascadeEnum, Type targetType, RelationType type, String name, String accessString, String table, String schema, Boolean lazy, Boolean inverse, String orderBy, String where, String sort, String columnKey, String[] compositeKeyColumnKeys, String element, Type elementType, String columnRef, String[] compositeKeyColumnRefs, IVisitable extraModel, String index, String indexType, CacheEnum cache, String cacheregion, NotFoundBehaviour notFoundBehaviour, FetchEnum fetch, Int32 batchSize, Type collectionType)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.VisitHasMany(HasManyModel model)
at Castle.ActiveRecord.Framework.Internal.HasManyModel.Accept(IVisitor visitor)
at Castle.ActiveRecord.Framework.Internal.AbstractDepthFirstVisitor.VisitNodes(IEnumerable nodes)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.VisitModel(ActiveRecordModel model)
at Castle.ActiveRecord.Framework.Internal.XmlGenerationVisitor.CreateXml(ActiveRecordModel model)
at Castle.ActiveRecord.ActiveRecordStarter.AddXmlToNHibernateCfg(ISessionFactoryHolder holder, ActiveRecordModelCollection models)
at Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder holder, IConfigurationSource source, IEnumerable`1 types, Boolean ignoreProblematicTypes)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource source, Type[] types)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(Assembly[] assemblies, IConfigurationSource source, Type[] additionalTypes)
at Castle.ActiveRecord.ActiveRecordStarter.Initialize(Assembly assembly, IConfigurationSource source)
at tests.Framework.ModelTests.SetUpFixture() in ModelTests.cs: line 18
This is my SetUpFixture
[TestFixtureSetUp]
public void SetUpFixture()
{
if (ActiveRecordStarter.IsInitialized) return;
IConfigurationSource source = ActiveRecordSectionHandler.Instance;
ActiveRecordStarter.Initialize(Assembly.Load("Model"), source);
XmlConfigurator.Configure();
}
I checked the files generated and details files has this filename Detail`1.hbm.xml backtick and this content:
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Contact.Detail`1, Model" table="Details" discriminator-value="0">
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
<generator class="native">
</generator>
</id>
<discriminator column="type" type="int" />
<property name="Owner" access="property" type="String">
<column name="Owner"/>
</property>
<property name="Location" access="property" type="String">
<column name="Location"/>
</property>
<property name="ContactDetail" access="property" type="String">
<column name="ContactDetail"/>
</property>
<many-to-one name="ParentContactInformation" access="property" class="Model.ContactInformation,Model" column="ParentContactInformation" lazy="proxy" />
<subclass name="Model.Contact.Email, Model" discriminator-value="3">
</subclass>
<subclass name="Model.Contact.Phone, Model" discriminator-value="1">
</subclass>
<subclass name="Model.Contact.Fax, Model" discriminator-value="2">
</subclass>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在尝试映射具有未定义类型参数的类(即开放泛型类)。您需要映射具体的类。有关详细信息,请参阅此内容:
The problem is that you're trying to map a class with an undefined type parameter (i.e. an open generic class). You need to map concrete classes. See this for more information:
因此,经过一些测试后,我发现我可以使用泛型类。问题是,
由于某种原因,我必须从 Detail 类移动到子类(电子邮件),BelongsTo 需要位于子类中。
So after some testing I found out that I can use generic classes. The problem was that I had to move
from my Detail class to the the child class (email) for some reason the BelongsTo needs to be in the children.