流畅的nhibernate映射复合主键部分引用

时间:2015-12-18 14:57:34

标签: c# nhibernate fluent-nhibernate

我正在尝试引用2个实体,因此我可以在查询中使用nhibernate加入它们。其中一个实体具有复合键,当仅在存在的字段上引用另一个实体时,我得到一个例外,表明外键的列数必须与主键的列数相同。 / p>

实体类:

public class StationData
{
    public virtual int GroupId { get; set; }
    public virtual int TypeId { get; set; }
    public virtual int code { get; set; }
    public virtual string Text { get; set; }

    protected bool Equals(StationAlarmText other)
    {
        if (other == null)
        {
            return false;
        }

        return GroupId == other.GroupId && TypeId == other.TypeId &&
               code == other.code;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != typeof(StationData))
        {
            return false;
        }
        return Equals((StationData)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = GroupId.GetHashCode();
            hashCode = (hashCode * 397) ^ TypeId;
            hashCode = (hashCode * 397) ^ Alarmcode;
            return hashCode;
        }
    }

}

另一个实体基类:

public class EventRecord
{
    public virtual DateTime TimeOn { get; set; }
    public virtual Group Group { get; set; }
    public virtual int Nr { get; set; }
    public virtual int Code { get; set; }
    public virtual EventType Type { get; set; }

    protected bool Equals(EventRecord other)
    {
        if (other == null)
        {
            return false;
        }

        return TimeOn.Equals(other.TimeOn) && Group == other.Group && Nr == other.Nr  && Code == other.Code && Type.Equals(other.Type);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != typeof (EventRecord))
        {
            return false;
        }
        return Equals((EventRecord) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = TimeOn.GetHashCode();
            hashCode = (hashCode*397) ^ (int) Group;
            hashCode = (hashCode*397) ^ Nr.GetHashCode();
            hashCode = (hashCode*397) ^ Code;
            hashCode = (hashCode * 397) ^ Type.GetHashCode();
            return hashCode;
        }
    }
}

此外,我已经创建了一个子类,因为在特定情况下我需要扩展实体来保存连接的值:

    public class EventRecordSub : EventRecord
{
    public virtual StationData StationData { get; set; }
}

现在的映射:

车站数据地图:

class StationDatatMap:ClassMap<StationData>
{
    public StationDatatMap()
    {
        Table("StationDataT");
        CompositeId().KeyProperty(x => x.code)
            .KeyProperty(x => x.GroupId).CustomType<int>();

        Map(x => x.TypeId);
        Map(x => x.Text);
    }
}

基本地图:

 public class EventRecordMap : ClassMap<EventRecord>
{
    public EventRecordMap()
    {
        Table("DataTbl");
        CompositeId()
            .KeyProperty(x => x.Group).CustomType<int>()
            .KeyProperty(x => x.Nr)
            .KeyProperty(x => x.Code)
            .KeyProperty(x => x.TimeOn, p => p.Type(NHibernateUtil.Timestamp.GetType()))
            .KeyProperty(x => x.EventType).CustomType<int>();

    }
}

EventRecordSub Map:

    public class EventRecordSubMap : SubclassMap<EventRecordSub>
{
    public EventRecordSubMap()
    {
        References(x => x.StationData).Columns("code","GroupId");
    }
}

我的问题是如何才能在指定的属性上进行这些表的完全内连接?为了清楚起见,数据库已经存在,无法重新设计。这真的很难吗?我可以编写一个查询来在sql中执行此连接,但无法找出正确的nhibernate和流畅的nhibernate配置。

我想继续使用QueryOver和joinAlias。

0 个答案:

没有答案