The first example of a ServiceStack database

I would like to start a new project using Servicestack with an existing database (SQL Server). I really like, as well as training on the multiservice tutorial. I would like to ask for some help, which is currently facing some problems, as I am a beginner, I am looking for an example / tutorial to start with POCO with an existing table mapping. a very simple example with one table and one service request will be enough to start.

thank

+4
source share
2 answers

First, to get an overview of the features of OrmLite, I recommend reading the documents on the OrmLite Home Page .

OrmLite ServiceStack, OrmLite NuGet, , InMemory, Sqlite NuGet:

Install-Package ServiceStack.OrmLite.Sqlite.Mono

* OrmLite.Sqlite.Mono Windows, Mono, Windows x86/x64 Sqlite OrmLite.Sqlite.Windows. SQL Server OrmLite.SqlServer .

, OrmLite ServiceStack, DB Factory RDBMS , :

public void Configure(Container container)
{
    container.Register<IDbConnectionFactory>(c => 
        new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
}

OrmLite POCO, 1:1 RDBMS, :

public class Simple
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? Age { get; set; }
}

using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
    //Drops (if exists) and re-creates SimpleTable
    db.DropAndCreateTable<Simple>();  
}

:

CREATE TABLE "Simple" 
(
  "Id" INTEGER PRIMARY KEY, 
  "Name" VARCHAR(8000) NULL, 
  "Age" INTEGER NULL 
); 

, , :

[Alias("CustomTable")]
public class Simple
{
    [AutoIncrement]
    public int Id { get; set; }

    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [Alias("CustomAge")]
    public int Age { get; set; }
}

SQL:

CREATE TABLE "CustomTable" 
(
  "Id" INTEGER PRIMARY KEY AUTOINCREMENT, 
  "Name" VARCHAR(100) NOT NULL, 
  "CustomAge" INTEGER NOT NULL 
); 

OrmLite API.

ServiceStack OrmLite ADO.NET Db Service. OrmLite POCO, , DTO ( DTO).

. ServiceStack Simple, Id, Simple:

public class MyServices : Service
{
    public object Any(Simple request)
    {
        return request.Id != default(int)
            ? Db.SingleById<Simple>(request.Id)
            : Db.Select<Simple>();
    }
}

ServiceStack OrmLite, :

+7

Source: https://habr.com/ru/post/1545934/


All Articles