Using LocalDB with Service Fabric

I have an Actor that after receiving a request from a WebAPI project, Actor queries a table using Entity Framework 6.

using (var context = new MetadataContext()) { var userStorageAccountId = context.Set<UsersToStorageAccounts>() .Find(userId).StorageAccountId; } 

The database was successfully created using the "Add-Migration" .. "Update-Database" commands and has the following connection string:

 @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFileName=C:\Users\xxxx\Metadata.mdf;Connect Timeout=30;Initial Catalog=Metadata;Integrated Security=True;" 

When I start the fabric service and the actor tries to access the context, I get the following exception:

When connecting to SQL Server, a network-related or specific instance error occurred. The server was not found or was not available. Verify the instance name is correct and configure SQL Server to connect remotely. (provider: SQL Network Interfaces, error: 50 - A local database error has occurred. Unable to create an automatic instance. For error information, see the Windows application event log.)

and event viewer error:

Unable to get local application data path. Most likely, the user profile is not loaded. If LocalDB is running in IIS, verify that profile loading is enabled for the current user.

I was looking for a way to load a user profile, but I found solutions for IIS.

Please, help:)

+5
source share
2 answers

Since the working fabric is working under a different user, you need to provide a common database and provide NETWORK SERVICE permissions:

  • When using the SqlLocalDB.exe command line , your connection issues the following command:

     sqllocaldb share MSSqlLocalDB SharedDB 
  • Open LocalDB using SQL Server Management Studio and go to the / Security / Logins section, add the local NETWORK SERVICE account and in user mapping add it as dbo (dbo.dbowner) to your database

  • Use the common name in the connection string as follows:

     "Data Source=(localdb)\.\SharedDB;Initial Catalog=[YOUR DB];Integrated Security=SSPI;" 

Edit Here you have a script that I added to my projects, change the database_name with the name of your db:

 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';" 
+12
source

Regarding IIS in the service structure, I thought that you cannot host an ASP.NET service application in IIS. Therefore, when you create a stateless ASP.NET SFTP project, the default host is Owin

-2
source

Source: https://habr.com/ru/post/1246404/


All Articles