Generate Primary Key for ASP.NET bound entity collections

Posts   
 
    
JuanV
User
Posts: 12
Joined: 08-Jul-2007
# Posted on: 08-Jul-2007 10:28:04   

I am using SQL Server 2005 with SelfServicing entities and ASP.NET 2.0.

On an ASP page, I have an LLBLGENDataSource connected to an EntityCollection. The datasource is bound to a DetailsView control.

When I debug and click "New" on the control and save it by clicking the "Insert" link I get this error:

ORMQueryExecutionException: An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'id' column does not allow nulls. INSERT fails. The statement has been terminated.

The problem is that the primary key is null. Of course I don't the user to have to input the primary key into a field every time they want to create a new entity. So my question is, how do I get the entity to automatically generate a primary key when the entity is saved and an insert is called? I tried putting this in the object constructor but it didn't seem to work:

id = Guid.NewGuid();

Thanks in advance!

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 08-Jul-2007 17:20:33   

JuanV wrote:

I am using SQL Server 2005 with SelfServicing entities and ASP.NET 2.0.

On an ASP page, I have an LLBLGENDataSource connected to an EntityCollection. The datasource is bound to a DetailsView control.

When I debug and click "New" on the control and save it by clicking the "Insert" link I get this error:

ORMQueryExecutionException: An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'id' column does not allow nulls. INSERT fails. The statement has been terminated.

The problem is that the primary key is null. Of course I don't the user to have to input the primary key into a field every time they want to create a new entity. So my question is, how do I get the entity to automatically generate a primary key when the entity is saved and an insert is called? I tried putting this in the object constructor but it didn't seem to work:

id = Guid.NewGuid();

Thanks in advance!

It sounds like what you want is to have the underlying table automatically generate the ID for you in the database? If that's the case, you need to modify the "id" coumn on that table to be an IDENTITY column.

Once that's done, you will need to refresh the catalog in the LLBL designer and regenerate the code.

HTH,

Phil

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Jul-2007 08:57:41   

If the PK is a GUID, then set the default value in the database to NEWSEQUENTIALID().

JuanV
User
Posts: 12
Joined: 08-Jul-2007
# Posted on: 19-Jul-2007 04:13:42   

Thanks for your excellent advice. I just rescripted the SQL DB and it works great.