This project is read-only.

Lint Function to Identify Missing Identity Attributes?

May 4, 2011 at 8:57 PM

I just got bitten again by a sort of subtle bug caused by a primary key field missing its autoincrement identity attribute. I'm not sure how the identity specification went missing, but I suspect it happened when I used the Copy Database wizard in SqlServer Management Studio to copy my development database onto a remote server for testing. In any event, it got lost. 

I understand why nHydrate can't add an autoincrement identity attribute to an existing field. But would it be possible to have the installer flag situations where the actual database schema doesn't match the nHydrate schema due to a missing autoincrement identity attribute? Sort of a lint-like function, as it were.

BTW, apologies if I've posted this suggestion before. It's just that I keep tripping over this particular problem, and it always takes me quite a while to remember to check for it.

- Mark

May 5, 2011 at 1:45 AM

There is not currently a way to validate the actual database schema (though this is a good idea); however you can verify that the API version matches the database version. There is a parameter for checking version T/F. This method will check the model key against the database. This is for the case where you have multiple databases and multiple models. When you check version it will also check the 5 digit version number major, minor, revision, build, and generated. This call will tell you if the database installer has been run against the database.




May 5, 2011 at 3:09 AM

Thanks, I was aware of the functionality you describe, but it wouldn't definitively catch the problem I ran into.

BTW, years ago I wrote a database abstraction layer that pulled all kinds of schema information out of existing SqlServer databases to configure data access for Windows Forms apps. I suspect that could still be done today with SqlServer 2008, although as I recall a number of the schema tables have been deprecated in favor of some newer stuff that doesn't cover as much ground.

Anyway, it might be fun to write a little lint checker for autoincrement keys. Is there a way to query the nHydrate schema programmatically for key information from some of the classes that get generated, or would I have to parse the schema file directly?

- Mark

May 6, 2011 at 3:23 AM

We do determine if a field is an identity on import so yes there is a way. You could look at the import code in this project. It is buried in there somewhere. You can also search Google for SQL Server, schema, identity, etc. and find queries people have posted.