Fluent NHibernate Mapping not on PK Field

淺唱寂寞╮ 提交于 2019-12-08 10:48:47

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/2192952/fluent-nhibernate-mapping-not-on-pk-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!