praeclarum / sqlite-net

Simple, powerful, cross-platform SQLite client and ORM for .NET
MIT License
4.08k stars 1.42k forks source link

Create different TableS dynamically with only one class definition #165

Open Perret opened 11 years ago

Perret commented 11 years ago

Hi

Is there any way to create different tables dynamically (i.e. without hardcoding many class definitions) based on one class (cause I don't think that new classes can be generated on fly), or at least same definition but with a different name?

Thanks

EDIT: and without using an command "CREATE TABLE " etc... i'm not sure if someone has suceeded to create Table suing some reflection tricks.

praeclarum commented 11 years ago

I have been tempted to add explicit table names, so you could do something like:

db.CreateTable ("AlphaThings"); db.CreateTable ("BetaThings"); db.CreateTable ("GammaThings");

etc.

Would that help?

Perret commented 11 years ago

Sometimes the table has to be created following a user definition, i.e the columns can't be hardcoded either. Well maybe there is no solution and I'm stuck with old fashion Strings =/.

Maybe a first trick could be to have a decent syntax for creating different table based on the same schema but with different name using something like: db.CreateTable("NameForThisTable") But then I guess it would create bunches of problem for the other methods... i.e. insert(MyClassDefinitionObject); //(but which table?)

louy commented 11 years ago

Did you try Inheritance?

public class MyClass
{
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [MaxLength(255)]
        public string Name { get; set; }
        // ...
}
public class MyClass2 : MyClass
{
}
Perret commented 11 years ago

Yes but can you generate new inherited classes without hardcoding?