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
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;