Azure Service Fabric: Connect to LocalDb

Today I set up my micro-services application to run locally. Even though Service Fabric SDK makes really simple to run the cluster on your machine, connecting to a SQL LocalDb is not so straight forward.

Service Fabric runs as a different user, so you need share your user localDb:

sqllocaldb share MSSqlLocalDB sharedlocal
sqllocaldb stop MSSQLLocalDB
sqllocaldb start MSSQLLocalDB

Create a proper login, running the following SQL on MSSQLLocalDB:

CREATE LOGIN [NT Authority\Network Service] FROM windows with DEFAULT_DATABASE=your_database_name;
USE your_database_name;
EXEC sp_addrolemember 'db_owner', 'NT Authority\Network Service'

Unfortunately this didn’t work for me:

Windows NT user or group 'NT Authority\Network Service' not found. Check the name again.
And I kind of deserve it, some how I should’ve used an Italian localized Windows image which localize also the local accounts name.

Based on your locale you can change the local account name in the query, for instance in my case is NT Authority\Servizio di Rete. Alternatively here a script which works on every locale:

DECLARE @NetSer VARCHAR(100)
SET @NetSer  = (SELECT SUSER_SNAME(0x010100000000000514000000))

        DECLARE @cmd VARCHAR(200)
        SET @cmd = N'CREATE LOGIN [' + @NetSer + '] FROM windows with DEFAULT_DATABASE=your_database_name'
        EXEC (@cmd)

use your_database_name;
exec sp_addrolemember 'db_owner', @NetSer;

Now using the following connection string your local Service Fabric cluster should be able to connect to your localDb.

Data Source=(localdb)\.\sharedlocal;Initial Catalog=your_database_name;Integrated Security=True;

Sources: #1 #2

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *