This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, i have two databases (a MySql and a SqLite) with the same structure. Both have the same tables an items.
For example there is a function "readTableXY(int id)":
public void readTableXY(int Id) { using (var sqLiteWorksapce = _SQLiteContext.CreateUnitOfWork()) { var Table = sqLiteWorksapce.TraceSimSipAccounts.Single(p => p.Id == Id); Console.WirteLine(Table.Id); Console.WirteLine(Table.Name); //Just for example } }
here i can give the id to my function and it will print the needed information out of the SqLite database (_SQLiteContext.CreateUnitOfWork()). Is there a way to select the database that i want to read out? So that the function accepted the name or the number of the database as a parameter, like public void readTableXY(int Database, int Id) ?
my first try / my firt idea was something like:
public void readTableXY(int Database, int Id) { TestDbUnitOfWork sqLiteWorksapce = _SQLiteContext.CreateUnitOfWork();
Console.WirteLine(Table.Name); ..... } }
...the idea was to cange the used workspace bevor start the using-block. But it does't work that way.
I hope you understand what i am looking for and that somebody has got a idea for me.
Thank you very much! Jörn |
|
|
If the application needs to be capable of running against either MySQL or SQLite, but each instance of the application uses only one database, then you can just do this by making the appropriate settings in the LightSpeedContext: public static readonly LightSpeedContext _context = new LightSpeedContext(); public void readTableXY(int id) { If you need to be able to talk to two different databases during the same instance of the application, you need two contexts, and you would then create a unit of work from whichever context was applicable at the time: public static readonly LightSpeedContext _mySqlContext = new LightSpeedContext(); public static IUnitOfWork CreateUnitOfWork(int dbType) { public void readTableXY(int dbType, int id) { By the way, to make your code more self-documenting, you might want to use an enum rather than an int to specify which database to read from. Just a suggestion! |
|
|
That works very god, thank you! I chose the second version with the two contexts. But now i've got another problem. As i said, there a two databases (MySql and SqLite) with the same structure. So all of the Tables and columns have got the same name - just diffenrent content. Now visual studio gives some error messages like: "Fehler 2 Der Typ "testDbNeu.TableXY" enthält bereits eine Definition für "_customer". " In english it means "Error code 2 - The type TableXY already contains a definition for _customer". Because both databases has got these variable "_customer" in the table "TableXY" for example. But i don't understand why. I created two LightSpeed modells, one for the SqLite db (SQLiteDB.lsmodel) and one for the MySql db (MySqlDB.lsmodel). So i have assumed, that there woud by a separation of these variables. So what can i do now? Do i have to rename all tables of one database (that woud be very awkward) or is there a way to seperate the Tables of the databaes without getting lost the conrol to talk to both of them? |
|
|
Hi, If the structure of the databases is the same, then you will only need to use *one* lsmodel file. The problem you're likely seeing is that because it's the same structure, and the classes are partial, they are merging into a single class that duplicates everything. So, one model file. This will provide one UnitOfWork. What you'll want to do is create two LightSpeedContext objects - each using the same UnitOfWork, however it is the LightSpeedContext that dictates which database is being used. I hope that makes sense :-) John-Daniel |
|
|
Thank you very much! ...again:) Sometimes it is so easy, that i'm not on it. I want to say, that these forum is very helpful and the answers are very precise |
|