Search code examples
c#nhibernatefluent-nhibernateautomapping

Map property as a value (not association) for using with IUserType conversion


I have this convention:

public class XmlSerializedConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType(typeof(XmlSerializedType<>).MakeGenericType(instance.Property.PropertyType));
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(
            x => Attribute.IsDefined(x.Property.MemberInfo, typeof(XmlSerializedDbMappingAttribute)));
    }
}

public class XmlSerializedType<T> : IUserType
{
    public bool IsMutable => true;

    public Type ReturnedType => typeof(T);

    public SqlType[] SqlTypes => new[] { NHibernateUtil.String.SqlType };
    // ...
}

which works well when I specify an override mapping.Map(x=>x.MyProperty) but without it I see an exception An association from the table X refers to an unmapped class: NotMappedType. My property is treated as an association (so never passed to the convention) but I want it to be treated as a normal value property.

[XmlSerializedDbMapping]
public NotMappedType MyProperty { get; set; }

How can I make it work without the override?


Solution

  • I changed XmlSerializedConvention to implement interface IUserTypeConvention (with no implementation changes) and now it works well.