在开发环境中运行Service Fabric群集时连接到本地SQL Server实例
在传统的控制台应用程序中打开与本地数据库的sql连接时,我没有任何问题,但是,当我尝试在Service Fabric中运行的无状态服务中执行相同的操作时,会出现登录错误.
When opening a sql connection to a local database in a traditional console app I have no issues, however when I attempt to do the same thing within a stateless service running in Service Fabric I get a login error.
我收到的错误是"Login failed for user 'WORKGROUP\\NICK$'."
这是我用来连接的代码
using (var con = new SqlConnection("Server=.;Trusted_Connection=True;Database=AddressBook"))
{
try
{
con.Open();
}
catch (Exception e)
{
}
}
当我尝试将该用户添加到sql server时,它告诉我找不到该用户.
When I try to add that user to sql server it tells me that the user cannot be found.
基于上面的评论,我了解到Service Fabric在NETWORK SERVICE帐户下运行.
Based on the comments above I learned that Service Fabric is running under the NETWORK SERVICE account.
解决方案是为要在SF群集中访问的数据库更新用户映射和角色成员身份.
The solution is to update the User Mapping and role membership for the databases that you want to access within the SF cluster.
在SSMS中,展开安全性",登录名",右键单击网络服务",然后选择属性.在用户映射"下,在要公开访问权限的每个数据库旁边,并在public
,db_datareader/writer
下放置一个复选框.
In SSMS expand Security, Logins, right click NETWORK SERVICE and then choose properties. Under User Mapping place a checkbox next to each Database that you want to expose access to and down below public
, db_datareader/writer
.