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
    0

    PetaPoco/Umbraco.Core.Persistence primary keys

    Hi

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

        [TableName("ForgottenPasswordRequests")]
        [PrimaryKey("Id", autoIncrement = true)]
        public class ForgottenPasswordRequest
        {
            [Column("Id")]
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int Id { getset; }
    
            [Column("Email")]
            public string Email { getset; }
    
            [Column("Guid")]
            public Guid Guid { getset; }
    
            [Column("Timestamp")]
            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
                    db.CreateTable<Way2Wellbeing.Data.Poco.ForgottenPasswordRequest>(false);
                }


    What am I missing?


    Thanks

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 22, 2014 @ 10:29
    Jeroen Breuer
    0

    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

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

    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: http://stackoverflow.com/questions/27094827/petapoco-how-to-turn-off-auto-increment

  • Ben Palmer 180 posts 866 karma points c-trib
    Jun 02, 2017 @ 09:51
    Ben Palmer
    1

    Did anyone ever find a fix for this?

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

    Works for me:

    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);
    
Please Sign in or register to post replies

Write your reply to:

Draft