How to create a table by using an object type in Dapper?

c# dapper

Question

I'm looking at how to make a blank table from an object type. There there anything ready to utilize despite the type's simplicity (it just has basic type properties)?

Should I build my own "CREATE TABLE" query utilizing some properties reflection?

As part of a project, I'm using Dapper to read data from a FirebirdSQL database and publish parts of those tables to a Sqlite database.

Any advice is much appreciated.

1
4
10/13/2019 8:36:59 AM

Accepted Answer

Dapper won't help you with that, as Marc said, but have a look at my response here (different data bases for ADO.NET) and modify one of the command attributes to create a CREATE TABLE line, which will make use of reflection. With that large of a head start, it ought to be rather easy.

I would provide an example, but I'm responding to your message on my phone.

9
5/23/2017 11:52:53 AM

Popular Answer

I know I'm a little late to the party, but I just wanted to mention that ServiceStack.OrmLite has this type of feature available:

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

However, kindly be informed 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