Copied to clipboard

Flag this post as spam?

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


  • Josip 187 posts 652 karma points c-trib
    Aug 26, 2019 @ 11:04
    Josip
    0

    Error when try to insert value in database with NPoco

    I am using this example from Umbraco docs and when i try to save values to the database table i am getting this error:˛

    Cannot insert the value NULL into column 'Id', table 'petapoco.dbo.BlogComments'; column does not allow nulls. INSERT fails. The statement has been terminated.

    Also, after table is created and i check it, I can see that primary key and autoincrement option is not set for id field.

    And this I how i insert values:

    public class Class1 : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<SubscribeToContentServiceSavingComponent>();
            }
            public class SubscribeToContentServiceSavingComponent : IComponent
            {
                public void Initialize()
                {
                    MemberService.Saved += MemberService_Saving;
                }
    
                public void Terminate()
                {
                }
    
                private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
                {
                    foreach (IMember member in e.SavedEntities)
                    {
                        var blogPostToAdd = new BlogCommentSchema();
    
                        blogPostToAdd.BlogPostUmbracoId = member.Id;
                        blogPostToAdd.Name = member.Name;
                        blogPostToAdd.Email = member.Name;
                        blogPostToAdd.Website = member.Name;
                        blogPostToAdd.Message = member.Name;
    
                        using (var scope = Current.ScopeProvider.CreateScope(autoComplete:true))
                        {
                            var database = scope.Database;
                            // use database  
                            scope.Database.Insert<BlogCommentSchema>(blogPostToAdd);
                            scope.Complete();
                        }
    
                    }
                }
            }
        }
    
  • Shaishav Karnani from digitallymedia.com 349 posts 1631 karma points
    Aug 26, 2019 @ 13:41
    Shaishav Karnani from digitallymedia.com
    100

    Hi,

    We have added PrimaryKeyColumn and that has worked to create table with Primary Key. Please can you try and confirm.

        [TableName("BlogComments")]
        [PrimaryKey("Id", AutoIncrement = true)]
        [ExplicitColumns]
        public class BlogCommentSchema
        {
            [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
            [Column("Id")]
            public int Id { get; set; }
    
            [Column("BlogPostUmbracoId")]
            public int BlogPostUmbracoId { get; set; }
    
            [Column("Name")]
            public string Name { get; set; }
    
            [Column("Email")]
            public string Email { get; set; }
    
            [Column("Website")]
            public string Website { get; set; }
    
            [Column("Message")]
            [SpecialDbType(SpecialDbTypes.NTEXT)]
            public string Message { get; set; }
        }
    }
    
  • Josip 187 posts 652 karma points c-trib
    Aug 26, 2019 @ 13:54
    Josip
    0

    Hi Shaishav,

    This is the missing part:

    [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
    

    You saved me a lot of time, thank you.

    BR

    Josip

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies