I'm getting this error "Cannot retrieve inserted id - no primary key was found." when trying to create a new record using editor.
I've searched the forums and found similar issues, but can't seem to apply these responses to a resolution:
http://mail.datatables.net/forums/discussion/45626/cannot-retrieve-inserted-id-no-primary-key-was-found
https://datatables.net/forums/discussion/53576/cannot-retrieve-inserted-id-no-primary-key-was-found
http://mail.datatables.net/forums/discussion/59623/cannot-retrieve-inserted-id
Our use case involves:
* .NET 6.0
* Editor 2.0.6
* SQL Server 2019
My web page has 2 tabs:
* UserInformation table to store personal info of users
* A SQL view joining 2 tables - UserInformation and UserAccess. The view edits Email and Access columns from UserAccess table using select dropdowns. For now I can edit current records, but have issue with creating a new one.
View model:
public class UserInfoAndAccessModel
{
public int UserAccessId { set; get; } // PK of UserAccess table
public string? FirstName { set; get; }
public string? LastName { set; get; }
public string? Title { set; get; }
public string? Email { set; get; }
public string? Access { set; get; }
}
UserInformation model:
public class UserInformationModel
{
public int UserId { set; get; }
public string? FirstName { set; get; }
public string? LastName { set; get; }
public string? Title { set; get; }
public string? Email { set; get; }
}
UserAccess model:
public class UserAccessModel
{
public int UserAccessId { set; get; }
public string? Email { set; get; }
public string? Access { set; get; }
}
Executing insert query directly in SQL Server did create new data. It just did not work from the web app. SQL tracker showed that the query was completed when I hit the create button, just no record was added, and the "no primary key" error popped up. Below is the insert query that was recorded:
exec sp_executesql N'INSERT INTO [VW_UserInfoAndAccess] ( [Access], [Email] ) VALUES ( @Access, @Email )',N'@Access nvarchar(7),@Email nvarchar(24)',@Access=N'Austria',@Email=N'abc@gmail.com'
View controller:
public class UserInfoAndAccessController : ControllerBase
{
[HttpGet]
[HttpPost]
public ActionResult UserInfoAndAccess()
{
string? cnStr = Program.ConnectionString;
using (var db = new Database("sqlserver", cnStr))
{
var editor = new Editor(db, "VW_UserInfoAndAccess", "UserAccessId")
.Model<UserInfoAndAccessModel>("VW_UserInfoAndAccess")
// Access column refers to CountryName column
.Field(new Field("VW_UserInfoAndAccess.Access").Options("Country", "CountryName", "CountryName")
.Validator(Validation.DbValues())
)
.Field(new Field("Country.CountryName").Set(false))
.LeftJoin("Country", "Country.CountryName = VW_UserInfoAndAccess.Access")
// UserAccess's Email refers to UserInformation's Email
.Field(new Field("VW_UserInfoAndAccess.Email").Options("UserInformation", "Email", "Email")
.Validator(Validation.DbValues())
)
.Field(new Field("UserInformation.Email").Set(false))
.LeftJoin("UserInformation", "UserInformation.Email = VW_UserInfoAndAccess.Email");
// Return data
return new JsonResult(editor.Process(Request).Data());
}
}
View js:
var editor = new $.fn.dataTable.Editor({
ajax: {
"url": '/api/UserInfoAndAccess',
"type": 'POST'
},
table: '#UserInfoAndAccess',
fields: [
{
"label": "Email",
"name": "VW_UserInfoAndAccess.Email",
"type": "select"
},
{
"label": "Access",
"name": "VW_UserInfoAndAccess.Access",
"type": "select"
}
]
});
var table = $('#UserInfoAndAccess').DataTable({
dom: 'Bfiprt',
orderCellsTop: true,
fixedHeader: true,
"lengthMenu": [[12, 24, 36, 48, 60, -1], [12, 24, 36, 48, 60, "All"]],
"pageLength": 60,
ajax: {
"url": '/api/UserInfoAndAccess',
"type": 'POST'
},
columns: [
{
"data": "VW_UserInfoAndAccess.FirstName"
},
{
"data": "VW_UserInfoAndAccess.LastName"
},
{
"data": "VW_UserInfoAndAccess.Title"
},
{
"data": "UserInformation.Email",
"editField": "VW_UserInfoAndAccess.Email"
},
{
"data": "Country.CountryName",
"editField": "VW_UserInfoAndAccess.Access"
},
],
select: true,
lengthChange: false,
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
],
});
Any ideas what I'm doing wrong? Many thanks in advance for any help.