Fluent NHibernate-HasOne映射到ReferencesAny

Fluent NHibernate-HasOne映射到ReferencesAny,nhibernate,fluent,has-one,Nhibernate,Fluent,Has One,我有以下POCO课程: public class Container { public virtual Int64 ContainerId { get; protected set; } public virtual string Name { get; set; } public virtual Location Location { get; set; } } public abstract class Location { public virtual I

我有以下POCO课程:

public class Container
{
    public virtual Int64 ContainerId { get; protected set; }
    public virtual string Name { get; set; }
    public virtual Location Location { get; set; }
}

public abstract class Location
{
    public virtual Int64 LocationId { get; protected set; }
    public virtual string Name { get; set; }
}

public class UniqueLocation : Location
{
    public virtual Container Container { get; set; }
}

public class SharedLocation : Location
{
    public SharedLocation()
    {
        this.Containers = new List<Container>();
    }

    public virtual IList<Container> Containers { get; set; }
}
公共类容器
{
公共虚拟Int64容器ID{get;protected set;}
公共虚拟字符串名称{get;set;}
公共虚拟位置{get;set;}
}
公共抽象类位置
{
公共虚拟Int64 LocationId{get;protected set;}
公共虚拟字符串名称{get;set;}
}
公共类唯一位置:位置
{
公共虚拟容器{get;set;}
}
公共类共享位置:位置
{
公共共享位置()
{
this.Containers=新列表();
}
公共虚拟IList容器{get;set;}
}
以及以下Fluent映射:

public class ContainerMap: ClassMap<Container>
{
    public ContainerMap()
    {
        Table("Containers");
        Id(x => x.ContainerId);
        Map(x => x.Name);
        ReferencesAny(x => x.Location).IdentityType<Int64>().EntityTypeColumn("LocationType").EntityIdentifierColumn("LocationId")
            .AddMetaValue<UniqueLocation>("U")
            .AddMetaValue<SharedLocation>("S");
    }
}

public class LocationMap : ClassMap<Location>
{
    public LocationMap()
    {
        Table("Locations");
        Id(x => x.LocationId);
        Map(x => x.Name);
    }
}

public class UniqueLocationMap : SubclassMap<UniqueLocation>
{
    public UniqueLocationMap()
    {
        HasOne(x => x.Container).PropertyRef(x => x.Location).ForeignKey("LocationId").Cascade.All().Constrained();
    }
}

public class SharedLocationMap : SubclassMap<SharedLocation>
{
    public SharedLocationMap()
    {
        HasMany(x => x.Containers).KeyColumn("LocationId");
    }
}
公共类容器映射:类映射
{
公共容器映射()
{
表(“集装箱”);
Id(x=>x.ContainerId);
Map(x=>x.Name);
ReferencesAny(x=>x.Location).IdentityType().EntityTypeColumn(“LocationType”).EntityIdentifierColumn(“LocationId”)
.AddMetaValue(“U”)
.AddMetaValue(“S”);
}
}
公共类位置映射:类映射
{
公共位置地图()
{
表(“位置”);
Id(x=>x.LocationId);
Map(x=>x.Name);
}
}
公共类UniqueLocationMap:子类映射
{
公共UniqueLocationMap()
{
HasOne(x=>x.Container).PropertyRef(x=>x.Location).ForeignKey(“LocationId”).Cascade.All().Constrained();
}
}
公共类SharedLocationMap:子类映射
{
公共共享位置映射()
{
有许多(x=>x.Containers);
}
}
问题是HasOne()映射生成以下异常:“Container.Location of:UniqueLocation,type Object需要2列,但映射了1列”


如何告诉HasOne()同时使用/映射LocationType和LocationId?

AFAIK,除了使用公式,实体引用上的条件是不可能的。这种设计看起来很奇怪,因为将一个独特的位置更改为一个共享的位置会很糟糕

您可以使用以下方法完成所需操作:

Reference(x => x.Container).Formula("(SELECT c.Id FROM Container c WHERE c.LocationId = Id AND c.LocationType = 'U')");
但我更愿意

class Location
{
    ...
    public virtual bool IsUnique { get { return Container.Count == 1; } }
}