ERROR Umbraco.Core.UmbracoApplicationBase - [Thread 10] An unhandled exception occurred
System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Id', table 'umbWay2Wellbeing_Dev.dbo.ForgottenPasswordRequests'; column does not allow nulls. INSERT fails.
The statement has been terminated.
In the database itself the field isn't defined as a PK after it has been created.
if (!db.TableExist("ForgottenPasswordRequests"))
{
//Create DB table - and set overwrite to false
db.CreateTable<Way2Wellbeing.Data.Poco.ForgottenPasswordRequest>(false);
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Web.DynamicData;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
[System.Web.DynamicData.TableName("TableName")]
public class TestClass
{
#region Table fields
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
public int NeededField { get; set; }
...
#endregion
#region Extra fields
[Ignore]
public bool ExtraField2 { get; set; }
...
#endregion
}
Usage:
var message = new TestClass()
{
Id = Guid.NewGuid(),
NeededField = 7377
};
var db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
db.Insert("TableName", "Id", false, message);
PetaPoco/Umbraco.Core.Persistence primary keys
Hi
I'm having issues with primary keys in a custom table:
But when I insert data I get:
In the database itself the field isn't defined as a PK after it has been created.
What am I missing?
Thanks
Hello,
I'm also running into the same problem. The primary key is set, but it still gives me this exception. Did you ever found out what the problem was?
Jeroen
I ran across this error too, in my case the PrimaryKey wasn't created at all. eg. The id column was just another 'int not null'
Sadly I've just had to create the table by hand, which is a great shame.
Comment author was deleted
Old thread, but we ran into a similar error.
We needed to explicitly set the PK value in a custom table.
The fix was to use this syntax:
db.Insert("articles", "article_id", false, a);
Where the args map like so:
table_name
,pk_column_name
,use_auto_increment
,object
Read this post for more info: http://stackoverflow.com/questions/27094827/petapoco-how-to-turn-off-auto-increment
Did anyone ever find a fix for this?
Works for me:
Usage:
is working on a reply...