Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of NPoco in .net Core

I have recently started diving into the new .net core along with asp.net core mvc. There have been several issues that I have come across but have been able to get most of them answered on my own. The one that has really stumped me is the use of NPoco.

How are you supposed to create the database instance?

The documentation reads:

IDatabase db = new Database("connStringName");
List<User> users = db.Fetch<User>("select userId, email from users");

This is not correct for DNXCORE50 as this constructor has been excluded for DNCORE50

I have also attempted this:

IDatabase _db = new Database(new SqlConnection(ConnStr));
_db.Single<string>("SELECT Username FROM dbo.Member");

When this code is ran I get a 'NullReferenceException'

Does anyone know how to get NPoco working properly?

like image 958
Radar5000 Avatar asked Jun 15 '16 15:06

Radar5000


1 Answers

There are other people having the same issue. This has been reported as issue #293 on NPoco GitHub repository.

The current workaround for this problem is to list the DbProviderFactory as shown below.

IDatabase _db = new Database(new SqlConnection(ConnStr),
                             DatabaseType.SqlServer2012, SqlClientFactory.Instance);
_db.Single<string>("SELECT Username FROM dbo.Member")
like image 133
Radar5000 Avatar answered Sep 22 '22 11:09

Radar5000