Dapper: create table from object type

c# dapper

Question

Good morning.

I'm trying to figure out how I can create an empty table from an object type. The type is quite simple (only primitive type properties); there's something ready to use? Do I need to make to write my own "CREATE TABLE" query using some properties reflection?

I'm working on a project that reads data from a FirebirdSQL database and writes some of that tables in a Sqlite database, using Dapper.

Any suggestions are appreciated.

Thank you!

Accepted Answer

As Marc stated, Dapper isn't going to do that for you, but have a look at my answer here (ADO.NET distinct data bases) and simply adapt one of the command properties to construct a CREATE TABLE statement and it will leverage reflection. It should be pretty straight forward with that much of a head start.

I'd create an example, but I'm answering this from my phone.


Popular Answer

Rather late to the party, but I just wanted to add that this kind of function is available in ServiceStack.OrmLite:

class Poco 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Ssn { get; set; }
}

db.DropTable<Poco>();
db.TableExists<Poco>(); //= false

db.CreateTable<Poco>(); 
db.TableExists<Poco>(); //= true

db.ColumnExists<Poco>(x => x.Ssn); //= true
db.DropColumn<Poco>(x => x.Ssn);
db.ColumnExists<Poco>(x => x.Ssn); //= false

Please be aware, however, that this library is not free.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow