Reputation: 3873
The documentation of the Sqlite-net says you can create a table using the ORM available in the library by using the generics as follow:
await connection.creatTableAsync<myClass>();
This will result in a table in the database named myClass
Now suppose that I need another table for storing the same type (myClass) but in a different table with different name (e.g.myOtherClassTable
).
How can this be done, using the Sqlite-net library?
Upvotes: 3
Views: 2867
Reputation: 6030
The API gives you only 2 options: int CreateTable<T>(CreateFlags createFlags = CreateFlags.None)
and int CreateTable(Type ty, CreateFlags createFlags = CreateFlags.None)
which will both create a table named by your type's name which is not what you want.
What you could do instead ist create the table on your own:
connection.Execute("CREATE TABLE [myOtherTable] (Column1 int, Column2 ...");
But, as @nvuono already said, this might give you some other trouble ;)
Upvotes: 2
Reputation: 3363
Now suppose that I need another table for storing the same type (myClass) but in a different table with different name
You can't do this.
The Table method requires that a table exists with the same name as the type being returned.
It's a simple ORM and not designed to care about any artificial limitations you are placing on it. It tracks the tables used based on the type name so would require another layer of indirection before it could deal with lookup tables to see a certain type being mapped to multiple tables.
What if I wanted to retrieve all rows in the database for the myClass object--should it return rows from both myClass table and myOtherClassTable? You would need to start writing more ORM functionality to provide options and figure all of that out which is exactly the kind of stuff that gets you into serious ORM territory.
Depending on the visibility of all your properties you could make a subclass type which doesn't actually implement any new functionality but just has a different class name or use a common IMyClass interface and actually have two different classes implementing the interface with different names but the same implementation.
None of those OO solutions are really ideal and if possible you should just consider adding another property to your class objects that you would filter on to determine which items you would have placed into the 2 separate tables.
Upvotes: 2