我在项目解决方案中有以下内容,我试图在一个地方维护Entity框架的数据库连接,这样如果我必须将数据库切换到不同的供应商(如MYSQL到MSSQL,反之亦然),我可以只需在一个地方更改连接名称,而不必在所有地方更改...我尝试了以下结构但遇到错误,如何解决?
项目#1
Dashboard.EntityFramework
-->bitDbConnection.cs
using
namespace Dashboard.EntityFramework
{
public class bitDbConnection
{
BitDatabaseEntities bitDB = new BitDatabaseEntities();
}
}
项目#2
Dashboard.Repository
-->Repository.cs
using Dashboard.EntityFramework
when I try use to bitDB variable I can the below error
错误: -
The name bitDB does not exist in current context
答案 0 :(得分:2)
这可能不是你想要的,但为了让你的代码能够工作,就这样写:
namespace Dashboard.EntityFramework
{
public class bitDbConnection
{
public BitDatabaseEntities bitDB = new BitDatabaseEntities();
}
}
using Dashboard.EntityFramework
public class Repository
{
public void DoSomething()
{
var bitDB = new bitDbConnection().bitDB;
}
}
因此,首先将bitDB字段设为public,然后使用它......
在评论中编辑问题:
public class Repository
{
private BitDatabaseEntities bitDB = new BitDatabaseEntities().bitDB;
public void DoSomething()
{
var x = bitDB.ToString();
}
}