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>
So after some testing I found out that I can use generic classes. The problem was that I had to move
[BelongsTo]
public ContactInformation ParentContactInformation { get; set; }
from my Detail class to the the child class (email) for some reason the BelongsTo needs to be in the children.