Composite key with auto generated column in Fluent NHibernate

251 views Asked by At

I have a table in a MySql database like this:

CertificateYear CHAR(4) (PK)
CertificateNumber INT AUTOINCREMENT (PK)
... other fields

the key is a composite one. I've used this configuration to have a field (CertificateNumber) which resets every year. Whenever the CertificateYear changes, CertificateNumber resets and starts from 1. My problem is on the mapping in Fluent NHibernate. So far, I've done this in my ClassMap:

CompositeId()
    .KeyProperty(x => x.CertificateYear, "CertificateYear")
    .KeyProperty(x => x.CertificateNumber, "CertificateNumber");

When saving an entity I set a value to CertificateYear and leave CertificateNumber unset, but I don't know how to say to NHibernate that CertificateNumber is auto-generated. It assigns a value of 0 to it, and then complains about a duplicate key... Question is: is there a way to tell to NHibernate that CertificateNumber, part of a composite key, is autogenerated?

0

There are 0 answers