Generate Quence for Field

Asked

Viewed 60 times

0

It is possible for me to create a quence without changing the field to id?

Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.DocumentoNumero);

Using this example, I would like to keep a quence for DocumentoNumero, but at the same time I have id that is already doing this and I need to keep it this way.

  • what is the purpose of documentNumero? is to be equal to id?

  • No, the id keeps generating in sequence for each document.. Documentnumero will not be unique, you should only generate a sequence when prompted or if it is saved as null

  • http://stackoverflow.com/questions/8990319/have-a-custom-setter-on-property-for-ef-code-first-model-entity I’m not sure, you want to have control over the "set" of that property. take a look at this link

2 answers

0


It’s been a while, but I’ll leave it here as I’ve decided to meet my need:

    protected void SetSequence(string sequenceName, int value)
    {
        string sqlQuery = string.Empty;

        if (value == 0)
            sqlQuery = $"SELECT setval('{sequenceName}', 1, false);"; // o próximo nextval() retornara {value} (1)
        else
            sqlQuery = $"SELECT setval('{sequenceName}', {value}, true);"; // o próximo nextval() retornara {value}+1

        Session.CreateSQLQuery(sqlQuery).ExecuteUpdate();
    }

0

  • almost that, in fact if it had any way to create a Quence (via nhibernate) without linking to the field, to be called by the application only when necessary would already be enough. The idea would be to maintain the integrity of this information, in case several users access at the same time

  • http://nhibernate.info/doc/howto/various/creating-a-custom-id-generator-for-nhibernate.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.