c# - 将 LocalDB 与 Service Fabric 结合使用

标签 c# entity-framework actor localdb azure-service-fabric

我有一个 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 权限:

  1. SqlLocalDB.exe命令行共享您的连接发出此命令:

    sqllocaldb share MSSqlLocalDB SharedDB
    
  2. 使用 SQL Server Management Studio 打开 LocalDB 并转到 /Security/Logins,添加 NETWORK SERVICE 本地帐户,然后在用户映射中将其添加为dbo (dbo.dbowner) 到你的数据库

  3. 像这样在连接字符串中使用共享名称:

    "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/

相关文章:

C# Selenium 循环遍历 Span 类 XPATH 中的输入按钮

entity-framework - 在 Entity Framework 中将int转换为字符串

entity-framework - 检查是否有任何待保存的更改

java - Akka:在 actor 系统之外进行通信?

Scala 模式匹配与 Option[Any] 混淆

unit-testing - 单元测试 Scala Actor

c# - ASP.NET MVC3 : Problems with forms, 未显示我的字段值

c# - 在 C# 中解析 dblp.xml 文件时出错

c# - 为什么 resharper 不建议删除多余的访问修饰符?

entity-framework - Entity Framework 中的查询投影是什么意思?