流利的NHibernate映射不在PK领域 [英] Fluent NHibernate Mapping not on PK Field

查看:294
本文介绍了流利的NHibernate映射不在PK领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  Person 
----- -
编号
编号
名称

编号
-----
编号PK
编号Person_Code
OrderDetails

现在在我的Person类中我想要一个订单列表,我不完全知道如何去设置流利的nhibernate映射匹配Code列而不是ID。没有外键约束,我无法将数据库更改为使用键。像这样的东西是我需要的,但似乎无法找出映射。

  public class Person 
{
public virtual int Id {get;组; }
公共虚拟字符串代码{get;组; }
公共虚拟IList< Order>订单{get;私人设置}
}

public class Order
{
public virtual int Id {get;组; }
公共虚拟字符串OrderDetails {get;组; }
public virtual Person Owner {get;组; }


解决方案

KeyColumn方法。它应该工作,不管是否存在外键约束。

  class PersonMap:ClassMap< Person> 
{
public PersonMap()
{
HasMany(p => p.Order)
.KeyColumn(Person_Code)
.PropertyRef 码);






PropertyRef方法可从 rev 614 ,因此您可能需要更新流利的nhibernate版本。


I have the following tables and cannot edit their structure...

Person
------
Id PK
Code
Name

Order
-----
Id PK
Person_Code
OrderDetails

Now in my Person class I want to have a list of Orders for that person, but I'm not entirely sure how to go about setting up the mapping in fluent nhibernate to match on the Code column rather than the ID. There is no foreign key constraint and I am unable to change the database to use the keys. Something like this is what I require, but can;t seem to figure out the mapping.

public class Person
{
    public virtual int Id { get; set; }
    public virtual string Code { get; set; }
    public virtual IList<Order> Orders { get; private set; }
}

public class Order
{
    public virtual int Id { get; set; }
    public virtual string OrderDetails { get; set; }
    public virtual Person Owner { get; set; }
}

解决方案

You define your column with the KeyColumn method. It should work regardless of existence of a foreign key constraint.

class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        HasMany(p => p.Order)
            .KeyColumn("Person_Code")
            .PropertyRef("Code");
    }
}

PropertyRef method is available from rev 614 so you may need to update the fluent nhibernate version.

这篇关于流利的NHibernate映射不在PK领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆