Primary Key or Identity field

Aug 6, 2012 at 4:54 AM


I have a table having a Primary Key (GUID) or may be INT

How can i insert or update the record including the Primary Key set manually based on business logic from the code using this API?


always having error and i had to automap it to ignore. But i do want to provide that primary key value from the code.




Aug 6, 2012 at 8:00 AM

This is fixed in this version: This version is in beta as I've been too busy the last couple of weeks to make a new release, however the code is stable. Instead of calling ExecuteReturnLastId you would call just Execute:

var productId = context.Insert("Product", product).AutoMap().Execute();

Hope this helps.


Aug 6, 2012 at 10:33 AM
Edited Aug 6, 2012 at 10:34 AM

If I am using GUID is my primary key and not assigning GUID from code but auto generate form database,  will it still return this newly create guid from database?

Aug 6, 2012 at 11:11 AM

No, the ExecuteReturnLastId only works with auto incremented numbers. If you want to return the GUID then you would have to do it manually using the context.Sql("insert into...."). However if possible you should generate the GUID in code and not in the database.

Aug 7, 2012 at 2:56 AM

so it means, i can not use GUID field as primary key on my table?

Aug 7, 2012 at 11:25 PM

If you want to use the built in Context.Insert("....") then you can use a GUID, but you must generate the GUID in the .NET code and not in the database. If you do it in the database and you use SQL Server then there is no way to return the newly generated GUID unless you generate it inside a stored procedure. So if you want to use a GUID, generate it in the .NET code and not in the database:

var product = new Product();
product.ProductId = Guid.NewGuid(); 
product.Name = "Apple iPad";
context.Insert("Product", product).Execute();