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 :
Is it possible to use a string as PK?
If yes, am I doing it right? So is there a bug somewhere in Umbraco DB or PetaPoco?
If no, there should be an exception thrown somewhere.
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.
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.
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 :
and then insert it using the following :
It works perfectly. But when I try to change the PK to use Code instead of Id :
and insert with :
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 :
Thanks
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
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
is working on a reply...