cannot retrieve inserted id
cannot retrieve inserted id

I am getting an error when trying to add a new record. the primary key is set as identity with a seed/increment. All the posts I see in the forum are talking about schemas but this one is just dbo. The primary key is not called "ID"
model:
public class LineItemCategoryModel
{
public int CategoryID { get; set; }
public string category { get; set; }
public string sortby { get; set; }
}
controller:
public class LineItemCategoriesController : ApiController
{
[Route("api/LineItemCategories")]
[HttpGet]
[HttpPost]
public IHttpActionResult Categories()
{
var request = HttpContext.Current.Request;
var settings = Properties.Settings.Default;
using (var db = new Database(settings.DbType, settings.DbConnection))
{
var response = new Editor(db, "InvoiceStructure_Categories", "CategoryID")
.Model<LineItemCategoryModel>()
.Process(request)
.Data();
return Json(response);
}
}
}
js
/**************************************************/
var categoryEditor = new $.fn.dataTable.Editor( {
ajax: '/api/LineItemCategories',
table: '#Categories',
fields: [
//{"label": "CategoryID:", "name": "CategoryID"},
{"label": "Category:", "name": "category"},
{"label": "SortBy:", "name": "sortby"}
]
} );
var categoryTable = $('#Categories').DataTable( {
dom: 'Bfrtip',
ajax: '/api/LineItemCategories',
columns: [
//{"data": "CategoryID"},
{"data": "category"},
{"data": "sortby"}
],
select: {style: 'single'},
lengthChange: false,
buttons: [
{ extend: 'create', editor: categoryEditor },
{ extend: 'edit', editor: categoryEditor },
{ extend: 'remove', editor: categoryEditor }
]
});
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You can rename the primary key with
idSrc
, see example here. Hopefully that'll do the trick,Colin
still getting same error.
is there something that I need to define in the controller or model telling it that sql is going to assign an autonumber?
oh my goodness!! So when the error says "No primary key found", go to your table and make sure you have your field set as Primary!!! Wow... I can't believe I missed that. I had identity seed and everything...except primary key!!
Sorry, thank you for your time.
Fantastic - great to hear you've got it working and the error messages actually helped rather than just obfuscating the problem (which I've seen all to often in error messages)!
Allan