功能NHibernate自动映射 - 2外键相同的表? [英] Fluent Nhibernate AutoMapping -- 2 foreign keys to same table?

查看:155
本文介绍了功能NHibernate自动映射 - 2外键相同的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我正在做一个基本的交易系统,我有以下的对象。

Let say I'm doing a basic transaction system where I have the following objects.

public class User
{
   public virtual int Id{get; set;}
}

public class Transaction
{
   public virtual int Id{get; set;}
   public virtual Item Item {get; set;}
   public virtual User Seller{get; set;}
   public virtual User Buyer{get; set;}
}

请注意我怎么有两个关系返回给用户对象。当FHN生成表模式我从事务表返回给用户表3 FK关系,Buyer_id,Seller_id,USER_ID

Notice how I have two relationships back to the User object. When FHN generates the table schema I get 3 FK relationships from the transaction table back to the User table, "Buyer_id", "Seller_id", "User_id"

我觉得这是自动生成的USER_ID字段错误地基于默认的事实,预计引用属性被称为用户

I think it's auto generating the "User_id" field erroneously based on the fact it by default expects the referencing property to be called "User"

使用FNH我怎么会指定此映射?

How would I specify this mapping using FNH?

推荐答案

Yarg!

我终于做了自动映射,当理解了它,你必须有两个独立的有很多映射指定覆盖

I finally figured it out when doing the auto mapping you have to specify an override with two separate Has Many mappings

   return Fluently.Configure()
    .Database(persistenceConfigurer)
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<User>()
            .Override<User>(map=> map.HasMany(user=> user.Transactions).KeyColumn("Buyer_id"))
            .Override<User>(map => map.HasMany(user => user.Transactions).KeyColumn("Seller_id"))                
        ))
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();

这篇关于功能NHibernate自动映射 - 2外键相同的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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