Manually setting identity field

Posts   
 
    
JMajidian
User
Posts: 2
Joined: 13-Feb-2007
# Posted on: 13-Feb-2007 20:54:42   

LLBLGen: 1.0.2004.2 Final (October 21, 2005) Runtime Libraries: .NET 1.1 Dev Env: VS.NET 2003 Database: SQL Server 2000 Templates: SelfServicing, two class scenario (Full / Safe)

I would like to manually set the ID of an Identity Field. I'm aware that LLBLGen Pro sets Identity Fields to readonly. Attempting to work around this, I used the ForcedCurrentValueWrite() method, but the SQL generated on Insert did not assign a the value.

Is there a work around to manually setting Identity fields?

Thanks, James

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 14-Feb-2007 07:55:01   

Setting an Identity Field on INSERT will cause a database error/exception.

Anyway if you force the value, try to set the field.IsChanged boolean to true.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 14-Feb-2007 10:22:42   

You can't set identity fields unless you set the Table manually into the mode to update identity fields. SqlServer has a statement for that: SET IDENTITY_INSERT. This isn't directly supported by LLBLGen Pro.

Frans Bouma | Lead developer LLBLGen Pro
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 11-Mar-2007 16:12:04   

Otis wrote:

You can't set identity fields unless you set the Table manually into the mode to update identity fields. SqlServer has a statement for that: SET IDENTITY_INSERT. This isn't directly supported by LLBLGen Pro.

I don't understand the answer. Firebird, for example, lets me easily change a primary key using an UPDATE statment and takes care of all foreign keys that are affected. I was trying to accomplish this with LLBLGen, but it didn't work. If I change the ID (works fine) and call adapter.SaveEntity(), nothing happens at all (There is not UPDATE statement generated at all). Is there a reason this doesn't work? The designer implies it should, as I can uncheck the "Is readonly" checkbox of my identity in the designer...

Identity handling seems to be a bit of a problem - there are quite a few postings that address these very issues, and since I started thinking about using LLBLGen, I already got 3 projects (of a total of 3 projects) coming where I need custom ID handling AND identities. I'm thinking about skipping Identity columns completely and calling my sequence procedures before saving my IDs manually - this would give me the freedom I need. Is there a recommended pattern to do so with a somewhat low overhead? Some thoughts I had:

  • before an entity is saved the first time, call the sequence (or an SP that calls the sequence and returns the ID). However, I'm not sure what this means in terms of overhead (I guess I had to acquire a connection each time).
  • before a save of a collection, call a procedure and request [x] IDs, where [x] is the number of new entities in the collection. The procedure would call the sequence [x] times and return all IDs at once. Then, I assign the IDs manually before calling adapter.SaveEntityCollection(). However, I don't know where to place this code.
  • [a better solution]

Thanks for the clarification

Philipp

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Mar-2007 20:28:29   

philipp wrote:

Otis wrote:

You can't set identity fields unless you set the Table manually into the mode to update identity fields. SqlServer has a statement for that: SET IDENTITY_INSERT. This isn't directly supported by LLBLGen Pro.

I don't understand the answer. Firebird, for example, lets me easily change a primary key using an UPDATE statment and takes care of all foreign keys that are affected. I was trying to accomplish this with LLBLGen, but it didn't work. If I change the ID (works fine) and call adapter.SaveEntity(), nothing happens at all (There is not UPDATE statement generated at all). Is there a reason this doesn't work? The designer implies it should, as I can uncheck the "Is readonly" checkbox of my identity in the designer...

The thing is that SQLServer (the Database you are using) have the IDENTITY_INSERT Flag, that tells the Database whether override or not that field insert . LLBLGen dont support that Flag, so isn't way to override the @@IDENTITY behavior.

I've encounter that scenario... This is a thread about some options wink : http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=8523

David Elizondo | LLBLGen Support Team
JMajidian
User
Posts: 2
Joined: 13-Feb-2007
# Posted on: 12-Mar-2007 17:32:52   

My interests in using LLBLGen Pro was to provide import/export functionality in my application. To do so, I needed to be able to set the identity column in a table.

Since LLBLGen Pro did not directly support this functionality, I decided to create a stored procedure that would use the Identity_Insert to create an empty row in the table with my desired id. Once created, I use an EntityClass to load the row and modify it as desired.

I hope this helps, James