AwkwardCoder
AwkwardCoder

Reputation: 25631

Fluent nHibernate no identity column in table

How do I specify with fluent NHibernate mapping for a table that doesn't have an identity column?

I want something like this:

public sealed class CustomerNewMap : ClassMap<CustomerNew>, IMap
{
    public CustomerNewMap()
    {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Not.Id(); // this is invalid...
        Map(x => x.Username);
        Map(x => x.Company);
    }
}

I mean no primary key defined in the database (not much defined in the database).

Upvotes: 8

Views: 13299

Answers (3)

AwkwardCoder
AwkwardCoder

Reputation: 25631

I found the answer to be:

  public CustomerNewMap()
  {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Id(x => x.Username).GeneratedBy.Assigned();
        Map(x => x.Company);
  }

Upvotes: 13

Simon Whitehead
Simon Whitehead

Reputation: 65049

This doesn't directly answer the question.. but it answers the issue in the comment to the accepted answer here.

We had the same issue with a SQL View over our Ledger. The problem is that NHibernate will happily duplicate rows when the Id property isn't unique. In our case.. we had Ledger entries that had the CustomerAccountId set as the Id.. which wasn't unique within the view. To get around that.. I mapped a CompositeId that was based on whatever I could find that made the row unique. In our case, it was a combination of CustomerAccountId and WeekEnding:

CompositeId()
    .KeyProperty(x => x.CustomerAccountId)
    .KeyProperty(x => x.WeekEnding);

This was enough to have NHibernate not map duplicates.

Upvotes: 6

Christopher Stott
Christopher Stott

Reputation: 1478

I found I had to explicitly set a column name in a similar case. Something like

  Id(x => x.Username).Column("Username").GeneratedBy.Assigned();

Upvotes: 0

Related Questions