I have a convention for my ids, which automatically maps properties with a name of Id as the identifier. As requirements are being fleshed out I need to tweak a domain model so naturally I went online and found that I need to create a class that inherits from IAutoMappingOverride<T>.
My convention:
public class PrimaryKeyConvention : IIdConvention, IIdConventionAcceptance
{
public void Apply(IIdentityInstance instance)
{
instance.Column("Id");
instance.GeneratedBy.SeqHiLo(instance.Name, "10");
}
public void Accept(IAcceptanceCriteria<IIdentityInspector> criteria)
{
criteria.Expect(x => x.Generator, Is.Not.Set);
}
}
My override:
public class LocateMappingOverride : IAutoMappingOverride<Locate>
{
public void Override(AutoMapping<Locate> mapping)
{
mapping.Map(x => x.SendTo).Not.Nullable();
}
}
The convention does work as expected if I remove my override.
The exception I get is The entity 'LocateMappingOverride' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)..
Is it possible to use conventions in conjunction with mapping overrides?
The answer is – yes, automapping can work with overrides.
Look what the error said. The problem is not with
Locateentity, but withLocateMappingOverrideentity, and that class should not be treated as entity, of course. You must haveIAutomappingConfigurationconfigured so that FluentNHibernate’s rule what to treat as entity includesLocateMappingOverride, too. And it does not have an Id mapped, indeed.You should either:
IAutomappingConfigurationso that classes that implementsIAutoMappingOverride<>are excludedIEntityand changeIAutomappingConfigurationrules respectively.