2
public class TransactionIdentityModelMapping : ClassMap<TransactionIdentityModel>
{
    public TransactionIdentityModelMapping()
    {
        Table("TransactionIdentities");
        Id(x => x.Id);
        References(x => x.Transaction);
    }
}

public class TransactionModelMapping : ClassMap<TransactionModel>
{
    public TransactionModelMapping()
    {
        Table("Transactions");
        Id(x => x.Id);
        HasMany(x => x.Identities);
    }
}

TransactionIdentityModel.Transaction is of type TransactionModel. This means there is a column created called "TransactionModel_id". How can I change this column name?

I have tried:

References(x => x.Transaction).Column("Transaction_id");

And:

References(x => x.Transaction).ForeignKey("Transaction_id");

And:

References(x => x.Transaction).Column("Transaction_id").ForeignKey("Id").Fetch.Join();

These all yield 2 columns, the one I want ("Transaction_id") and the original ("TransactionModel_id").

Do I need to also do something with the HasMany?

Edit the models:

public class TransactionModel
{
    /// <summary>
    /// For NHibernate
    /// </summary>
    protected TransactionModel()
    {
    }

    public static TransactionModel FromId(TransactionIdentityModel tranIdentity)
    {      
        return new TransactionModel
            {
                Identities = new List<TransactionIdentityModel> { tranIdentity }
            };
    }

    public virtual Guid Id { get; protected set; }

    public virtual IList<TransactionIdentityModel> Identities { get; protected set; }
}

public class TransactionIdentityModel
{        
    public virtual Guid Id { get; protected set; }

    public virtual TransactionModel TransactionModel { get; set; }
}

Yielding 2 columns, this is the SQL being generated if I use the map: References(x => x.Transaction).Column("Transaction_id").ForeignKey("Id").Fetch.Join();

alter table TransactionIdentities 
    add constraint Id 
    foreign key (Transaction_id) 
    references Transactions

alter table TransactionIdentities 
    add constraint FK958B77026F5C4B80 
    foreign key (TransactionModel_id) 
    references Transactions
weston
  • 54,145
  • 21
  • 145
  • 203

1 Answers1

2

Assuming Transaction_Id is the foreign key to the related TransactionModel then you need to change your References statement to this:

 References(x => x.Transaction).Column("Transaction_id");

This will override nHibernates default expectation for the field to be TransactionModel_id

Looking at this question Prevent Nhibernate schemaexport from generating foreign key constraints on has many relationship, I think you will have to change your HasMany mapping to this:

 HasMany(x => x.Identities).KeyColumn("Transaction_id")
Community
  • 1
  • 1
connectedsoftware
  • 6,987
  • 3
  • 28
  • 43
  • Didn't work I ended up with two, one called "Transaction_id" and one called "TransactionModel_id". Is there anything I need to do on the `ClassMap` where it has a `HasMany` link? – weston Jun 14 '13 at 13:11
  • @weston could you post your class definitions for `TransactionModel` and `TransactionIdentityModel` please – connectedsoftware Jun 17 '13 at 12:34
  • @weston thanks the one question I should have asked originally, when you say this "yields 2 columns" where exactly?, when you check the SQL that is being executed? Are you experiencing a run-time error somewhere? – connectedsoftware Jun 17 '13 at 13:27
  • In the sql that is generated to create the tables I see both old and new columns, see edit on question. – weston Jun 17 '13 at 15:31
  • @weston see updated answer - I did some research and found another question with a similar problem - hopefully my revised answer will work or point you in the right direction! – connectedsoftware Jun 17 '13 at 15:37
  • Thanks for all your help, that got me on the right track, have edited answer to the one that worked. – weston Jun 18 '13 at 08:24