Primary Key or Identity field

Aug 6, 2012 at 3:54 AM

Hi 

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.

 

regards,

TIN

Coordinator
Aug 6, 2012 at 7:00 AM

This is fixed in this version: http://fluentdata.codeplex.com/releases/view/90977. 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 9:33 AM
Edited Aug 6, 2012 at 9: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?

Coordinator
Aug 6, 2012 at 10: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 1:56 AM

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

Coordinator
Aug 7, 2012 at 10: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();