How to map composite primary key to foreign in fluent nhibernate?

社会主义新天地 提交于 2019-12-06 06:30:47

问题


I have the following tables:

table A:

 FOO (PK) | CLIENT (PK)

table B:

 BAR (PK) | CLIENT (PK/FK) | FOO (FK)

PK -> primary key

FK -> foreign key

There's a one-to-many relation between A and B. I can't simply do this:

class AMap
{
    public AMap()
    {
        CompositeId().KeyReference(a => a.FOO)
                     .KeyReference(a => a.CLIENT);
        HasMany(a => a.B);
    }
}


class BMap
{
    public BMap()
    {
        CompositeId().KeyReference(a => a.BAR)
                     .KeyReference(a => a.CLIENT);
        References(a => a.A);
    }
}

It will fail with the following exception:

Foreign key (FKE7804EB3DA7EBD4B:B [FOO])) must have same number of columns as the referenced primary key (A [FOO, CLIENT])

Is it possible to map this correctly with fluent nhibernate?


回答1:


Found the Solution:

HasMany(a => a.B).KeyColumns.Add("FOO", "CLIENT").Cascade.All(); 


来源:https://stackoverflow.com/questions/4460648/how-to-map-composite-primary-key-to-foreign-in-fluent-nhibernate

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