Is it possible to define a descending index in OrmLite? I can only see the [Index] attribute but I have a table of over 1 million records and need a descending index.
If it's for a composite index you can specify it within its name:
[CompositeIndex("Field1", "Field2 DESC")]
public class Table
{
...
public string Field1 { get; set; }
public string Field2 { get; set; }
}
Otherwise you can use a Pre/Post Custom SQL Hooks, e.g:
[PostCreateTable("CREATE INDEX IX_NAME ON MyTable (Field1 DESC);")]
public class MyTable
{
...
public string Field1 { get; set; }
public string Field2 { get; set; }
}
Which will execute the Post SQL Hook to create the index after the table is created.
Related
I am using version 4.5.14 of Servicestack ormlite
here "InMaintenance" Property is ignored as it is not the "Network" table column in the database. I want to set the value of the InMaintenance property based on whether the "Enddate" column in the NetworkMain table has value or not.
Following is the code
but the above code generates the following SQL query for SelectExpression
as we can see there is no space between the not null condition in the above expression.
And FromExpression is as follows
I know that I can use the SQL query in the select but how to resolve this issue?
Thanks!
Amol
4.5.14 is several years old, but this generates valid SQL in the latest version of OrmLite. Here's a live demo on Gistlyn you can run:
OrmLiteUtils.PrintSql();
public class Network
{
[PrimaryKey]
public string Id { get; set; }
public string Name { get; set; }
[Ignore]
public bool InMaintenance { get; set; }
}
public class NetworkMain
{
[PrimaryKey]
public string Id { get; set; }
[ForeignKey(typeof(Network))]
public string NetworkId { get; set; }
public DateTime? EndDate { get; set; }
}
public class NetworkDTO
{
public string Id { get; set; }
public string Name { get; set; }
public bool InMaintenance { get; set; }
}
var q = db.From<Network>()
.LeftJoin<NetworkMain>()
.Select<Network, NetworkMain>((a, m) => new
{ a,
InMaintenance = m.NetworkId == a.Id && m.EndDate.HasValue ? "1" : "0"
}).OrderBy(x=>x.Name);
var results = db.Select<NetworkDTO>(q).ToList();
Which generates:
SELECT "Network"."Id", "Network"."Name", (CASE WHEN (("NetworkMain"."NetworkId"="Network"."Id")AND("NetworkMain"."EndDate" is not null)) THEN #0 ELSE #1 END) AS InMaintenance
FROM "Network" LEFT JOIN "NetworkMain" ON
("Network"."Id" = "NetworkMain"."NetworkId")
ORDER BY "Network"."Name"
I have a model of the following form:
public class LanguageText
{
[PrimaryKey]
public int Id { get; set; }
public string de { get; set; }
public string en { get; set; }
public string ru { get; set; }
}
How can I query just one column by Id? I tried this:
SQL = "SELECT [de] from [LanguageText] WHERE [Id] = \"1\""
var p = App.Database.QueryAsync<LanguageText>(SQL).Result.First();
This will return one whole row of LanguageText in p, while I want the contents of the [de] row as string only.
How do I accomplish this?
As we found out
App.Database.ExecuteScalarAsync<string>(SQL).Result
Is there a way to create a composite non-unique index in Servicestack's implementation of Ormlite?
For example, a single index that would cover both Field1 and Field2 in that order.
public class Poco
{
[AutoIncrement]
public int Id { get; set; }
...
public string Field1 { get; set; }
public string Field2 { get; set; }
}
You can use the CompositeIndexAttribute, e.g:
[CompositeIndex("Field1", "Field2")]
public class Poco
{
[AutoIncrement]
public int Id { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
}
I don't know how to store collection (Comments) in separate table.
By default comments are serialized and stored in SomeClass table as column Comments.
[{Id:0,CreateDate:2013-09-12T14:28:37.0456202+02:00,,SomeClassID:1,CommentText:"coment text",}]
Is there any way to save it in separate tables?
public class SomeClass {
[AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
List<Comment> comments = new List<Comment>();
public List<Comment> Comments {
get { return comments; }
set { comments = value; }
}
}
public class Comment {
[AutoIncrement]
public int Id { get; set; }
[References(typeof(SomeClass))]
public int SomeClassID { get; set; }
[StringLength(4000)]
public string CommentText { get; set; }
}
I don't think ORMLite supports serializing to multiple tables. 1 table = 1 class so the comments will be stored as a Blob field in the SomeClass table.
If you need to store them in separate tables you will have to save the comments separately and have a foreign key reference back to the id of the SomeClass table.
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)