如何使用具有集成安全性和来自同一 MVC EF 应用的 SQL 登录的 SQL Server 连接?



我构建了一个内部网托管的 MVC/EF/C# 应用程序,该应用程序使用 Windows 身份验证和标识模拟,并使用集成安全性连接到 SQL Server。EF 连接字符串如下所示:

<add name="BgEntities" connectionString="metadata=res://*/Models.Bg.csdl|res://*/Models.Bg.ssdl|res://*/Models.Bg.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxxxxxx;initial catalog=Bg_Dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

这对于普通应用程序用户非常有用。 我现在需要向 Intranet 上的所有用户打开一个控制器,但我无法将公司中的每个人作为用户添加到数据库中。我想使用通用 SQL Server 登录名从此控制器访问数据库。 是否可以在此控制器中切换连接字符串?

所以对我有用的是根据 Rodioon 的帖子在数据库上下文上设置连接字符串 此处 配置多个数据库实体框架 6。

我相信有更好的方法,我假设这首先使用 web.config 中指定的连接字符串连接到数据库,然后使用第二个连接字符串重新连接。但它得到了结果。

private bg db = new bg();
public ActionResult Index(int? id)
{
db.Database.Connection.ConnectionString = "data source=xxx;initial catalog=bg;MultipleActiveResultSets=True;User Id=xxx;Password=yyy;App=EntityFramework";

;

最新更新