Fluent Nhibernate AutoMapping — 2 foreign keys to

2019-02-26 02:27发布

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

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"

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"

How would I specify this mapping using FNH?

1条回答
何必那么认真
2楼-- · 2019-02-26 03:16

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();
查看更多
登录 后发表回答