Ferdinando Santacroce
Ferdinando Santacroce

Reputation: 1077

Create table from object type

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.

Upvotes: 5

Views: 12112

Answers (2)

David Brower
David Brower

Reputation: 3048

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.

Upvotes: 4

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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.

Upvotes: 9

Related Questions