简体   繁体   English

流利的NHibernate-分离表映射

[英]Fluent NHibernate - Detached table mapping

Is there a way I can setup mapping for a table that doesn't have a direct reference to another table? 有没有一种方法可以为没有直接引用到另一个表的表设置映射? It actually gets it's reference from another table that I do have a direct reference from. 实际上,它是从我确实有直接引用的另一个表中获得的。

This is what I have so far, but I'm not sure how to map the "LookupValue" in my MetaData model. 到目前为止,这是我所拥有的,但是我不确定如何在我的MetaData模型中映射“ LookupValue”。 It would need to map to MetaData if the [mdd].DefinitionType equals the [mdl].LookupType and the [md].DataValue equals the [mdl].LookupKey. 如果[mdd] .DefinitionType等于[mdl] .LookupType,并且[md] .DataValue等于[mdl] .LookupKey,则需要映射到MetaData。

public class MetaData {
    public virtual long TableID { get; set; }
    public virtual MetaDataDefinition Definition { get; set; }
    public virtual int DefinitionID { get; set; }
    public virtual String DataValue { get; set; }
    public virtual MetaDataLookup LookupValue { get; set; }

    public override bool Equals(object obj) { ... }
    public over int GetHashCode() { ... }
}

public class MetaDataDefinition {
    public virtual long ID { get; set; }
    public virtual string DefinitionName { get; set; }
    public virtual string DefinitionType { get; set; }
}

public class MetaDataLookup {
    public virtual string Type { get; set; }
    public virtual string LookupKey { get; set; }
    public virtual string LookupValue { get; set; }

    public override bool Equals(object obj) { ... }
    public over int GetHashCode() { ... }
}

public class MetaDataMap : ClassMap<MetaData> {
    public MetaDataMap() {
        Table("PPOMetaData");
        CompositeId()
            .KeyProperty(x => x.TableID, "TableID")
            .KeyProperty(x => x.DefinitionID, "DefinitionID");

        References(x => x.Defintion, "DefinitionID").Not.LazyLoad().Cascade.All().Fetch.Join();
        Map(x => x.TableID);
        Map(x => x.DataValue);
    }
}
public class MetaDataDefinitionMap : ClassMap<MetaDataDefinition> {
    public MetaDataDefinitionMap() {
        Table("MetaDataDefinitions");
        Id(x => x.ID);
        Map(x => x.DefinitionName);
        Map(x => x.Type);
    }
}

public class MetaDataLookupMap : ClassMap<MetaDataLookup> {
    public MetaDataLookupMap() {
        CompositeId()
            .KeyProperty(x => x.LookupType)
            .KeyProperty(x => x.LookupKey);
        Map(x => x.LookupValue);
    }
}

Ideally, I want to have it run a query similar to this: 理想情况下,我希望它运行类似于以下的查询:

SELECT     data.TableID, data.DefinitionID, def.DefinitionName, data.DataValue,lu.LookupValue AS DataValue
FROM         dbo.PPOMetadata AS data 
    INNER JOIN dbo.MetaDataDefinitions AS def ON def.ID = data.DefinitionID 
    LEFT OUTER JOIN dbo.MetaDataLookup AS lu ON lu.LookupType = def.Type AND lu.LookupKey = data.DataValue
WHERE data.TableID = 1

In terms of update ability, the only thing I would ever create, update or delete would be in the MetaData table. 就更新能力而言,我唯一会创建,更新或删除的内容将在MetaData表中。 The definitions and Lookup values would never change (at least from this part of the application). 定义和查找值将永远不会改变(至少从应用程序的这一部分开始)。 Is mapping the "MetaDataLookup" directly to the MetaData model possible? 是否可以将“ MetaDataLookup”直接映射到MetaData模型? If so, can someone point me in the right direction of what I should be looking at? 如果是这样,有人可以指出我应该看的正确方向吗?

Thanks! 谢谢!

I came up with a workaround that seems to be working and might take some of the complexity out. 我想出了一个可行的解决方法,可能会消除一些复杂性。 Instead of trying to handle the complex joins in a ClassMap, I built a view in Sql Server that does this for me. 我没有尝试处理ClassMap中的复杂联接,而是在Sql Server中构建了一个为我执行此操作的视图。 In my application, I built a new Model and ClassMap for the view. 在我的应用程序中,我为视图构建了一个新的Model和ClassMap。 I haven't implemented any update logic yet, but I think I'll have the update logic work directly on the MetaData model, while the read logic (which needs everything joined together) will use the new MetaDataView model. 我还没有实现任何更新逻辑,但是我认为我将让更新逻辑直接在MetaData模型上工作,而读取逻辑(需要将所有内容结合在一起)将使用新的MetaDataView模型。

I'm still curious if complex joins like this are possible in Fluent NHibernate, but for now this solution seems to be working for me. 我仍然很好奇在Fluent NHibernate中是否可以进行像这样的复杂连接,但是现在这种解决方案似乎对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM