How to use Dapper in ServiceStack

前端 未结 2 1286
-上瘾入骨i
-上瘾入骨i 2021-02-20 10:31

Currently, I am using OrmLite for DB operations. I am also planning to use Dapper ORM, but can anyone point me how to integrate DapperORM in ServiceStack. Do I need to implement

2条回答
  •  暖寄归人
    2021-02-20 11:33

    Dapper works like OrmLite in that they're both extension methods over the underlying ADO.NET System.Data.* IDbConnection interface. This means you can use both of them together on the IDbConnection retrieved from OrmLite's IDbConnectionFactory.

    OrmLite includes a recent version of Dapper in ServiceStack.OrmLite under the ServiceStack.OrmLite.Dapper namespace.


    In v3.9.40 of ServiceStack, this embedded version of Dapper had their APIs changed to include a 'Dapper' suffix to avoid clashing with OrmLite methods of the same name. You don't have to register a Request-Scoped IDbConnection as ServiceStack's default Service class already retrieves it from the IDbConnectionFactory for you.

    Given that, here's how you can access Dapper ORM APIs inside a ServiceStack service:

    public class MyService : Service
    {
        public object Any(Request request)
        {
            base.Db.QueryDapper(...);
            base.Db.QueryMultipleDapper(...);
            base.Db.MultiMapDapper(...);
            base.Db.ExecuteDapper(...);
        }
    }
    

提交回复
热议问题