EF如何将连接字符串传递到其他项目

时间:2018-07-11 08:09:08

标签: c# entity-framework connection-string dbcontext

我有一个针对不同项目的解决方案。我在以存储库模式构成的'DALProject'中使用EF6。我需要做的是从另一个引用了上述项目的名为“ MainProject”的项目中进行CRUD操作,但是我收到此错误:`

No connection string named 'MyEntity' could be found in the application config file

要解决这个问题,我的想法是将连接字符串直接传递给DBContext构造函数。 因此,我在外部文件中复制了DALProject的App.Config中的连接字符串,然后从MainProject中读取此文件。这是连接字符串:

metadata=res://*/My_Model.csdl|res://*/My_Model.ssdl|res://*/My_Model.msl;provider=System.Data.SqlClient;provider connection string="data source=192.xxx.xxx.xxx\SQL2008R2EXP;initial catalog=My_Catalog;persist security info=True;user id=sa;password=xxxxxx;multipleactiveresultsets=True;application name=EntityFramework" 

完成此操作后,我尝试以这种方式设置DBContext:

public class My_Entities : DBContext{
    public My_Entities(string connStr) 
          : base (connStr){}
}

但是我得到的是这个错误:`

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.`

有帮助吗?

0 个答案:

没有答案