I am using ServiceStack version="3.9.54" targetFramework="net40" with PostgreSQL.\
When i create table with
public class test
{
[AutoIncrement]
public int id { get; set; }
public string test_name { get; set; }
}
dbConn.CreateTable<test>(true);
CREATE TABLE test
(
id serial NOT NULL,
test_name text,
CONSTRAINT test_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE);
But when i create with
public class test
{
public string test_name { get; set; }
[AutoIncrement]
public int id { get; set; }
}
dbConn.CreateTable<test>(true);
Here is table on Postgres
CREATE TABLE test
(
test_name text NOT NULL,
id integer NOT NULL,
CONSTRAINT test_pkey PRIMARY KEY (test_name)
)
WITH (
OIDS=FALSE
);
What happen with my id columns. Is it bug ?
Thanks for your help
Tuan Hoang Anh
I think there are some conventions and case-sensitivity at play here. If you change id to Id it should work
public class test
{
public string test_name { get; set; }
[AutoIncrement]
public int Id { get; set; }
}
dbConn.CreateTable<test>(true);
OrmLite expects an 'Id' property to be present and to be the primary key. You can attribute a property with [PrimaryKey] if you don't want to use Id. However, in this case attributing id with [PrimaryKey] will attempt to create 2 primary keys since OrmLite can't find an Id field and (I think) defaults the first property it finds to be the primary key (can't find docs/proof to back this up, though)
Related
I have created a table in Dynamo Db, with Id as the primary key and customerID as sortkey.
When i query an item by Id as shown below, I get error "the provided key element does not match the schema"
var db = new PocoDynamo(awsDb);
db.GetItem("aa4f0371-6144-4bd9-8980-5066501e37aa");
When I remove the sortkey from the dynamo DB, it works as expected.
What is the correct way to get an item by Id, which also has a sort key associated with it.
public class Notification
{
[PrimaryKey]
public Guid Id { get; set; }
[RangeKey] //Sort Key
public Guid CustomerId { get; set; }
public Guid LinkId { get; set; }
public string PreviewText { get; set; }
}
In PocoDynamo you can specify both Hash Key and Range Key with a [CompositeKey] attribute, e.g:
[CompositeKey(nameof(Id), nameof(CustomerId))]
public class Notification
{
public Guid Id { get; set; }
public Guid CustomerId { get; set; }
public Guid LinkId { get; set; }
public string PreviewText { get; set; }
}
I'm looking for a way to map several POCO objects into single table in the ServiceStack.
Is it possible to do this in a clean way, without "hacking" table creation process?
As a general rule, In OrmLite: 1 Class = 1 Table.
But I'm not clear what you mean my "map several POCO objects into single table", it sounds like using Auto Mapping to populate a table with multiple POCO instances, e.g:
var row = db.SingleById<Table>(id);
row.PopulateWithNonDefaultValues(instance1);
row.PopulateWithNonDefaultValues(instance2);
db.Update(row);
If you need to maintain a single table and have other "sub" classes that maintain different table in the universal table you can use [Alias] so all Update/Select/Insert's reference the same table, e.g:
public class Poco
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
[Alias(nameof(Poco))]
public class PocoName
{
public int Id { get; set; }
public string Name { get; set; }
}
[Alias(nameof(Poco))]
public class PocoAge
{
public int Id { get; set; }
public int Age { get; set; }
}
Although I don't really see the benefit over having a single table that you use AutoMapping to map your other classes to before using that in OrmLite.
I have to work off an existing Db & would like to use ServiceStack's OrmLite.
Thus I have created Poco classes, using OrmLite T4 templates.
ISSUE: I would like to save to a table which has multiple primary keys.
public partial class DbUserGroup
{
[Required]
public int Userid { get; set;} // this is a primary key
[Required]
public int Groupid { get; set;} // this is a primary key
public int Ranking { get; set;}
public bool Isprimary { get; set;}
}
Currently using Db.Save(userGroup) does not work. Is there any way of addressing this using ServiceStack's OrmLite.
Multiple primary keys don't exist. A multi-column primary key yes.
Please take a look on this link https://github.com/ServiceStack/ServiceStack.OrmLite#limitations
As it said
A potential workaround to support tables with multiple primary keys is to create an auto generated Id property that returns a unique value based on all the primary key fields
I resolved it by adding [PrimaryKey] to both properties.
public partial class DbUserGroup
{
[Required]
[PrimaryKey]
public int Userid { get; set;} // this is a primary key
[Required]
[PrimaryKey]
public int Groupid { get; set;} // this is a primary key
public int Ranking { get; set;}
public bool Isprimary { get; set;}
}
I'm very new to Entity Framework, so I apologize if this is a stupid question...
Here is my POCO:
[Table("RegistrationCodes")]
public class RegistrationCode
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public Guid Code { get; set; }
public int? UserId { get; set; }
[ForeignKey("UserId")]
public UserProfile User { get; set; }
[StringLength(500)]
public string Notes { get; set; }
}
When I retrieve the object from my DBSet with the code below, UserId is always null.
return this.Context.RegistrationCodes.FirstOrDefault(c => c.Code == code);
I've checked the database and verified that the UserId is not null. Additionally, i have profiled and can see that the UserId is being requested from the database.
Any help would be greatly appreciated.
Ohhhhh my, don't I feel stupid...
Here's what was happening, I run the code and I can see the UserId field is updated in the database. Every time I restart the application, code migrations would run add do an AddOrUpdate to the table which would null out the UserId column.
Can we just delete this question all together?
There seems to be hardly any examples out there so here goes:
Here are my three structures but it doesn't seem to create the tables properly and when I call the following line it says Id is not recognised:
IEnumerable<Permission> permissions = _data.Find<RolePermission>(x => x.Role.RoleKey == roleKey).Select(x => x.Permission);
RolePermission:
public class RolePermission
{
[SubSonicPrimaryKey]
public int RolePermissionId { get; set; }
public int RoleId { get; set; }
public int PermissionId { get; set; }
//Foreign Key of Role
public Role Role { get; set; }
//Foreign key of Permission
public Permission Permission { get; set; }
}
Permission:
public class Permission
{
[SubSonicPrimaryKey]
public int Id { get; set; }
[SubSonicLongString]
public string PermissionKey { get; set; }
[SubSonicLongString]
public string PermissionDescription { get; set; }
}
Role:
public class Role
{
[SubSonicPrimaryKey]
public int Id { get; set; }
[SubSonicLongString]
public string RoleKey { get; set; }
[SubSonicLongString]
public string RoleDescription { get; set; }
}
I don't know if this has been fixed in a current release but I remember a silly bug in subsonic's primary key detection.
If your Object contains a property named Id subsonic assumes that is your primary key.
If not you have to tell subsonic with is your PK by decorating a property with the SubSonicPrimaryKey attribute (like you did).
If you have a property called Id and it is also decorated with the attribute (like your Role and Permission class) subsonic finds the property twice and does not check if they both equals. Then it throws an exception because it can't reliably determine which one to take.
Long story short, you should try:
a) Remove the Attribute from your Id column
b) Rename the property to RoleId or PermissionId (which would be more consistend because your RolePermission class has it's PK called RolePermissionId)
If that doesn't help, please provide a stacktrace.