问题
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:
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();
来源:https://stackoverflow.com/questions/1423377/fluent-nhibernate-automapping-2-foreign-keys-to-same-table