0

I work with one application connected with 3 databases same structure just the data inside different. I used WinForms with entity framework 6 .Net not Core when user login he choose the database. how I made function to send dbcontext or dbset from chosen database? for example I have a Site table have same columns and same names after user login need to fill datagridview with data from database that user choose. I did it but with duplicate the code for every database :(

  • _"3 databases same structure just the data inside different"_ I think I understood that part. But not this part: _" I did it but with duplicate the code for every database"_ What is "the code for every database" exactly when those three databases are having the same structure/i.e. meta data? – Stefan Wuebbe Jun 29 '22 at 16:32
  • the structure of the application is login form with 3 options for user to choose the dbase after login there a datagridview show data from the chosen database. in my way I have 3 Dbcontext and coding with if depends on Connectionstring for example if I want to bring data: if(CString == "CS1") {var con = (from a in db1.site select a).tolist();} if else(CString == "CS2") {var con = (from a in db2.site select a).tolist();} if else(CString == "CS3") {var con = (from a in db3.site select a).tolist();} that the meaning of duplication. need a general function to send entity to process there. – ÃHmêd Ãlzáwï Jun 29 '22 at 18:54
  • I see, would suggest to put the additional info into the Question itself by editing it so that readers can see it immediately, and also for the readability of the included bring-data code of your recent Comment – Stefan Wuebbe Jun 29 '22 at 19:09
  • If the database itself is reusable among the three sites, and the application code is not yet, at first glance it looks to me as if it could be possible to move the different-site config values to the App.config or appsettings.json respectively, what do you think? – Stefan Wuebbe Jun 29 '22 at 19:14
  • i've just started coding that's why I didn't include it in the question I'll add more explainmation to the question. I already have a three connection strings in app.config, the user take only one when he login. I think it's easy with functions when you send a variables, nevertheless Complicated with EF, I think I'll try it with Repository. Thank you. – ÃHmêd Ãlzáwï Jun 29 '22 at 19:42

1 Answers1

0

If the databases are truly identical, then you should only need to use one of three different connectionstrings when initializing the dbcontext. Maybe have the first one as the default one for entity framework for building models etc.

Here is an example of how to set the connectionstring in code: Entity Framework 6 set connection string in code

Good luck!!

steb
  • 436
  • 3
  • 12
  • The default connection string depends on user choose after login. need a general function to send the context whatever the source. ex: var context = (from a in ???.users select a).tolist(); this ??? chanable, depends on what database that user use and the 3 models have the same .users dbset with different data. – ÃHmêd Ãlzáwï Jun 29 '22 at 20:14