Copied to clipboard

Flag this post as spam?

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


  • Pau 14 posts 75 karma points
    May 11, 2015 @ 12:49
    Pau
    0

    Problems with petapoco

    Hi guys, i'm starting with petapoco to save my categories, buy im having a problem with the autoincrement,

    I followed this example: http://www.nibble.be/?p=440, but i dont create nodes with the categories, i show them in a table, loading them from the DB, and everyone have Id = 0

    This is my model:

    [TableName("categories")]
        [PrimaryKey("CategoryId", autoIncrement = true)]
        public class Category
        {
            public Category() { }
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int CategoryId { get; set; }
            public string Name { get; set; }
            public string Father { get; set; }
            public override string ToString()
            {
                return "Category: "+ Name ;
            }
       

    This is my RegisterEvents

    public class RegisterEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var db = applicationContext.DatabaseContext.Database;
                if (!db.TableExist("categories"))
                {
                    db.CreateTable(false);
                }
            }
        }

    And the controller:

    public class CategoryApiController : UmbracoApiController
        {
            public IEnumerable GetAll()
            {
                var query = new Sql().Select("*").From("categories");
              return DatabaseContext.Database.Fetch(query);
            }
            public Category GetById(int id)
            {
                var query = new Sql().Select("*").From("categories").Where(x => x.CategoryId == id);
                return DatabaseContext.Database.Fetch(query).FirstOrDefault();
            }
            public Category PostSave(Category cat)
            {
                if (cat.CategoryId > 0)
                 DatabaseContext.Database.Update(cat);
             else
                 DatabaseContext.Database.Save(cat);
             return cat;
            }
            public int DeleteById(int id)
            {
                return DatabaseContext.Database.Delete(id);
            }
        }

    i don't see where is the error, but i can't edit or delete items, because them all have the same id

     

  • Robert Sikkens 5 posts 76 karma points
    Mar 03, 2016 @ 09:29
    Robert Sikkens
    0

    Same here, how did you solve this?

Please Sign in or register to post replies

Write your reply to:

Draft