Pregunta

I have a question about correctly handling a recreation of a database.

In my dev environment I often recreate the database by using

_schemaExport.Drop(true, true);
_schemaExport.Create(createResult, true);

(I have to note, that I use the hilo generator.) Right after when I recreated the database, sometimes a save of a new entity fails because of "Cannot insert duplicate key..." exception.

My question: Would I have to reinitialize the session factory (and maybe even the session) to correctly come in sync with the new hilo-using database? Or sould it work just as is?

Any hint is appreciated!

Lg
warappa

¿Fue útil?

Solución

I'd say you definitely have to create a new session after recreating a database. Another option is to clear the existing one before recreating DB.

The ID generator will start from scratch after you've recreated the DB. This can cause some generated ID to be the same as ID of another object in previously existing session. Thus you're getting duplicate key errors.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top