简体   繁体   English

如何处理 Dbcontext 和 Dbset?

[英]how deal with Dbcontext and Dbset?

I work with one application connected with 3 databases same structure just the data inside different.我使用与 3 个相同结构的数据库连接的一个应用程序,只是内部的数据不同。 I used WinForms with entity framework 6 .Net not Core when user login he choose the database.当用户登录他选择数据库时,我使用 WinForms 和实体框架 6 .Net 而不是 Core。 how I made function to send dbcontext or dbset from chosen database?我如何制作从所选数据库发送 dbcontext 或 dbset 的功能? 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.例如,我有一个站点表在用户登录后具有相同的列和相同的名称需要用用户选择的数据库中的数据填充 datagridview。 I did it but with duplicate the code for every database :(我做到了,但是每个数据库都重复了代码:(

If the databases are truly identical, then you should only need to use one of three different connectionstrings when initializing the dbcontext.如果数据库确实相同,那么在初始化 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下面是如何在代码中设置连接字符串的示例: Entity Framework 6 set connection string in code

Good luck!!祝你好运!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM