本文介绍了流利的NHibernate映射多个非PK字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况类似于



但是,我的表之间的关系由多个非主键列描述。
想象一下Chris Meek的情况,但是 Person 有一个 JobType 和一个代码 (对不起,这是一个遗留数据库)唯一地描述了 Person

  Person 
------
Id PK
JobType
代码
名称

订单
-----
Id PK
Person_JobType
Person_Code
OrderDetails

SerhatÖzgel的描述了使用 PropertyRef ,但我无法找到一个方法来单独做多列。我试过类似于

  class PersonMap:ClassMap< Person> 
{
public PersonMap()
{
HasMany(p => p.Order)
.KeyColumns.Add(Person_JobType)
。 PropertyRef(JobType)
.KeyColumns.Add(Person_Code)
.PropertyRef(Code)
}
}

但是这显然不起作用,因为 KeyColumns.Add()返回另一个 OneToManyPart 所以 PropertyRef()不会针对要添加的单个列运行。第二个 PropertyRef()只是覆盖第一个,我得到以下错误:

  NHibernate.MappingException:集合外键映射
列数错误:MyApp.Person.Order类型:Int32

我查看了KeyColumns.Add(),

  public TParent添加(字符串名称)
public TParent添加(params string []名称)
public TParent添加(字符串columnName,动作< ColumnPart> customColumnMapping)
公共TParent添加(ColumnMapping列)

特别是最后两个,但找不到任何方法设置 PropertyRef 每个列的单独级别:(
是否有办法做到这一点?我是完全错误的方式吗?

解决方案
使用hbm.xml和FluentNHibernate可能有一个小窍门

  class PersonMap:ClassMap< P erson> 
{
public PersonMap()
{
Map(_ => JobTypeAndCode)
.Columns.Add(Person_JobType,Person_Code)
.ReadOnly()
.LazyLoad()//可选:防止加载列两次
.Access 。没有();

HasMany(p => p.Orders)
.KeyColumns.Add(Person_JobType,Person_Code)
.PropertyRef(JobTypeAndCode)
}

私人对象JobTypeAndCode {get;组; } // FakeProperty

$ / code $ / $ p

注意:我从来没有使用NHibernate MappingByCode


I have a situation similar to that described in Fluent NHibernate Mapping not on PK Field

However, the relationship between my tables is described by multiple non-primary key columns. Imagine Chris Meek's situation but where a Person has a JobType and a Code that, together, should (sorry, it's a legacy database) uniquely describe a Person

Person
------
Id PK
JobType
Code
Name

Order
-----
Id PK
Person_JobType
Person_Code
OrderDetails

Serhat Özgel's answer describes using PropertyRef, but I can't find a way to do that individually for multiple columns. I've tried similar to

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        HasMany(p => p.Order)
            .KeyColumns.Add("Person_JobType")
            .PropertyRef("JobType")
            .KeyColumns.Add("Person_Code")
            .PropertyRef("Code")
    }
}

But this obviously doesn't work, since KeyColumns.Add() returns another OneToManyPart so PropertyRef() isn't being run against the individual column being added. The second PropertyRef() simply overwrites the first one, and I get the following error:

NHibernate.MappingException : collection foreign key mapping
has wrong number of columns: MyApp.Person.Order type: Int32

I've looked at the various overloads of KeyColumns.Add(),

public TParent Add(string name)
public TParent Add(params string[] names)
public TParent Add(string columnName, Action<ColumnPart> customColumnMapping)
public TParent Add(ColumnMapping column)

Specifically the last two, but couldn't find any way to set PropertyRef individually level for each column :(Is there a way to do that? Am I going about this the wrong way entirely?

解决方案

using hbm.xml and FluentNHibernate it is possible with a trick

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Map(_ => JobTypeAndCode)
            .Columns.Add("Person_JobType", "Person_Code")
            .ReadOnly()
            .LazyLoad() // optional: prevent loading the Columns twice
            .Access.None();

        HasMany(p => p.Orders)
            .KeyColumns.Add("Person_JobType", "Person_Code")
            .PropertyRef("JobTypeAndCode")
    }

    private object JobTypeAndCode { get; set; } // FakeProperty
}

Note: i never got this to work using NHibernate MappingByCode

这篇关于流利的NHibernate映射多个非PK字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:56