功能NHibernate自动映射 - 2外键相同的表?功能、NHibernate

由网友(丶先生拉着我的手白头到老)分享简介:让说我正在做一个基本的交易系统,我有以下的对象。Let say I'm doing a basic transaction system where I have the following objects.public class User{public virtual int Id{get; set;}}p...

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

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();
阅读全文

相关推荐

最新文章