我有一个 Actor,在收到来自 WebAPI 项目的请求后,Actor 使用 Entity Framework 6 查询一个表。
using (var context = new MetadataContext())
{
var userStorageAccountId = context.Set<UsersToStorageAccounts>()
.Find(userId).StorageAccountId;
}
使用“Add-Migration”..“Update-Database”命令成功创建了数据库,并具有以下连接字符串:
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFileName=C:\Users\xxxx\Metadata.mdf;Connect Timeout=30;Initial Catalog=Metadata;Integrated Security=True;"
当我运行结构服务并且参与者尝试访问上下文时,我得到以下异常:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)
事件查看器错误是:
Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
我寻找加载用户配置文件的方法,但我只找到了 IIS 的解决方案。
请帮忙:)
最佳答案
由于服务结构在另一个用户下运行,您需要共享数据库并授予 NETWORK SERVICE
权限:
与 SqlLocalDB.exe命令行共享您的连接发出此命令:
sqllocaldb share MSSqlLocalDB SharedDB
使用 SQL Server Management Studio 打开 LocalDB 并转到 /Security/Logins,添加
NETWORK SERVICE
本地帐户,然后在用户映射中将其添加为dbo (dbo.dbowner) 到你的数据库像这样在连接字符串中使用共享名称:
"Data Source=(localdb)\.\SharedDB;Initial Catalog=[YOUR DB];Integrated Security=SSPI;"
编辑 这里有一个我添加到我的项目中的脚本,将 databasename 更改为您的数据库名称:
sqllocaldb create MSSQLLocalDB
sqllocaldb start MSSQLLocalDB
sqllocaldb share mssqllocaldb sharedlocal
sqllocaldb stop MSSQLLocalDB
sqllocaldb start MSSQLLocalDB
"c:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S "(localdb)\.\sharedlocal" -d "databasename" -Q"create login [nt authority\network service] FROM windows with DEFAULT_DATABASE=databasename;use databasename;exec sp_addrolemember 'db_owner', 'nt authority\network service';"
关于c# - 将 LocalDB 与 Service Fabric 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401991/