Copied to clipboard

Flag this post as spam?

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


  • Vincent Vallet 2 posts 32 karma points
    Mar 10, 2015 @ 11:15
    Vincent Vallet
    0

    Inserting rows in Umbraco Database with varchar PK

    Hi,

    I'm not sure I'm in the right section but I didn't find a relevant one so here I am.

    I'm quite new to Umbraco, and starting a new project with it.

    At the moment I'm working on importing data from an Excel file in custom tables in the Umbraco Database.

    The problem I have is that for some tables I'd like to use a varchar PK, but the insertions won't work.

    If I define my table as follows :

    [TableName("CustomCountry")]
    [ExplicitColumns]
    public class CustomCountry
    {
        [Column("Id")]
        [PrimaryKeyColumn(AutoIncrement = true)]
        public int Id { get; set; }
        [Column("Code")]
        public string Code { get; set; }
        [Column("Name")]
        public string Name { get; set; }
    }
    

    and then insert it using the following :

    UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
    CustomCountry c = new CustomCountry { 
        Code = "FRA",
        Name = "France"
    };
    db.Save(c);
    

    It works perfectly. But when I try to change the PK to use Code instead of Id :

    [TableName("CustomCountry")]
    [ExplicitColumns]
    public class CustomCountry
    {
        [Column("Code")]
        [PrimaryKeyColumn(AutoIncrement = false)]
        public string Code { get; set; }
        [Column("Name")]
        public string Name { get; set; }
    }
    

    and insert with :

    UmbracoDatabase db = ApplicationContext.DatabaseContext.Database;
    CustomCountry c = new CustomCountry { 
        Code = "FRA",
        Name = "France"
    };
    db.Save("CustomCountry", "Code", c);
    

    It doesn't work. But I have no exception thrown! It seems to work but the table stays empty.

    I have also tried using a string column named Id as PK, so I can use "db.Save(c);" but it doesn't work either. I have tried inside and outside a transaction, doesn't change anything.

    So for the moment I am forced to have those useless int autoincrement PK on my tables, that I'd like to get rid of.

    If I understood well, Umbraco Database extends PetaPoco for the database layer, so I guess it could come from Umbraco or PetaPoco...

    So, to sum up :

    1. Is it possible to use a string as PK?
    2. If yes, am I doing it right? So is there a bug somewhere in Umbraco DB or PetaPoco?
    3. If no, there should be an exception thrown somewhere.

    Thanks

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 10, 2015 @ 12:04
    Jeroen Breuer
    100

    Hello,

    I haven't tried to use a string as a PK but since it's just PetaPoco there are probably some docs about it. Have you looked at the Umbraco source code? Maybe there is a table which already does the same thing.

    Jeroen

  • Vincent Vallet 2 posts 32 karma points
    Mar 11, 2015 @ 10:35
    Vincent Vallet
    0

    Hello Jeroen,

    Thanks for the hint, I found out that the umbracoUser2app table is using a composite int / varchar PK, and they use db.Insert instead of db.Save, so I tried this on my own table and it works for me as well.

    So my problem is solved, but IMO there is still an issue that should be looked into with the db.Save method.

    Thanks for your help.

    Vincent

Please Sign in or register to post replies

Write your reply to:

Draft