Copied to clipboard

Flag this post as spam?

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


  • Josip 195 posts 662 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 354 posts 1638 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 195 posts 662 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

  • Dennis 75 posts 398 karma points MVP 2x
    Apr 29, 2022 @ 13:06
    Dennis
    0

    This is an old post, but for anyone passing by:

    Do not use autoComplete:true if you're performing write operations to your database inside your scope. Instead, explicitly call scope.Complete() before closing the using statement.

    Here's why:
    autocomplete causes your scope to complete, no matter what happens inside the using statement. If you throw an exception between write operations and you have autocomplete on, your database might end up in an invalid state!

    With autocomplete set to false, you are guaranteed that your changes to the database are applied only if the entire operation succeeds.

    This also applies if you're only doing one operation inside your scope. Scopes may be nested and if your scoped operation fails, but auto complete is on, a parent scope might think that the operation was successful and you'll invalidate your database.

    So basically: Never use autoComplete:true unless you're 100% sure what you're doing.

Please Sign in or register to post replies

Write your reply to:

Draft