Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 327
AwkwardCoder Avatar asked Aug 17 '09 13:08

AwkwardCoder


2 Answers

I found the answer to be:

  public CustomerNewMap()
  {
        WithTable("customers_NEW");
        Not.LazyLoad();
        Id(x => x.Username).GeneratedBy.Assigned();
        Map(x => x.Company);
  }
like image 198
AwkwardCoder Avatar answered Sep 16 '22 12:09

AwkwardCoder


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.

like image 42
Simon Whitehead Avatar answered Sep 18 '22 12:09

Simon Whitehead