2

我有以下 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; }
}

和以下流利映射:

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");
    }
}

问题是 HasOne() 映射生成以下异常:“损坏的列映射:Container.Location of: UniqueLocation,类型 Object 需要 2 个列,但映射了 1 个”。

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

4

1 回答 1

0

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; } }
}
于 2013-05-03T08:58:32.930 回答