Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • James Woodruff 6 posts 27 karma points
    Apr 03, 2014 @ 17:08
    James Woodruff

    PetaPoco/Umbraco.Core.Persistence primary keys


    I'm having issues with primary keys in a custom table:

        [PrimaryKey("Id", autoIncrement = true)]
        public class ForgottenPasswordRequest
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int Id { getset; }
            public string Email { getset; }
            public Guid Guid { getset; }
            public DateTime Timestamp { getset; }

    But when I insert data I get:

    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

    What am I missing?


  • Jeroen Breuer 4902 posts 12234 karma points MVP 3x admin c-trib
    Sep 22, 2014 @ 10:29
    Jeroen Breuer


    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?


  • Jonathan Richards 288 posts 1742 karma points MVP
    Nov 28, 2014 @ 12:47
    Jonathan Richards

    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

    Jan 22, 2016 @ 16:28

    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:

  • Ben Palmer 173 posts 819 karma points c-trib
    Jun 02, 2017 @ 09:51
    Ben Palmer

    Did anyone ever find a fix for this?

  • Iegor 1 post 20 karma points
    Jun 13, 2017 @ 14:24

    Works for me:

    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Web.DynamicData;
    using Umbraco.Core.Models;
    using Umbraco.Core.Persistence;
    public class TestClass
        #region Table fields
        public Guid Id { get; set; }
        public int NeededField { get; set; }
        #region Extra fields
        public bool ExtraField2 { get; set; }


    var message = new TestClass()
        Id = Guid.NewGuid(),
        NeededField = 7377
    var db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
    db.Insert("TableName", "Id", false, message);
Please Sign in or register to post replies

Write your reply to: