Can auto mappings conventions work with mapping overrides?

寵の児 提交于 2019-12-12 16:05:48

问题


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?


回答1:


The answer is - yes, automapping can work with overrides.

Look what the error said. The problem is not with Locate entity, but with LocateMappingOverride entity, and that class should not be treated as entity, of course. You must have IAutomappingConfiguration configured so that FluentNHibernate's rule what to treat as entity includes LocateMappingOverride, too. And it does not have an Id mapped, indeed.

You should either:

  • change your IAutomappingConfiguration so that classes that implements IAutoMappingOverride<> are excluded
  • move the override outside the scope that is searched for entities
  • or introduce a common marker interface that all entities need to implement, i.e. IEntity and change IAutomappingConfiguration rules respectively.


来源:https://stackoverflow.com/questions/6413767/can-auto-mappings-conventions-work-with-mapping-overrides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!