title: SqlException: Cannot open database "path\db.mdf" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\SYSTEM'
body: <p>I know that this question have been already asked many times but I looked through the answers and haven't found solution for my problem. </p>
  
  <p>I've built a <code>Windows Service</code> in C# that connects to the database and fill it with data from some other source. I set <code>Account -&gt; LocalSystem</code> for <code>ServiceProcessInstaller</code> and <code>StartType -&gt; Automatic</code> for <code>ServiceInstaller</code>. </p>
  
  <p>I use Entity Framework to generate .edmx from database, this is a connection string:  </p>
  
  <pre><code>&lt;add name="ConStringName"
           connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;
       provider=System.Data.SqlClient;provider connection string='data source=(LocalDB)\v11.0;
       attachdbfilename=&amp;quot;somePath\MyDbName.mdf&amp;quot;;
       integrated security=True;connect timeout=30;
       MultipleActiveResultSets=True;
       App=EntityFramework'"
           providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>And I have this code:  </p>
  
  <pre><code>var objContext = ((IObjectContextAdapter)dbContext).ObjectContext;
  objContext.ExecuteStoreCommand("DELETE FROM Properties;");
  </code></pre>
  
  <p>On the second line I get</p>
  
  <pre><code>Cannot open database "path\DbName.MDF" requested by the login. The login failed.
  Login failed for user 'NT AUTHORITY\SYSTEM'.
  </code></pre>
  
  <p><strong>What I've tried so far:</strong>  </p>
  
  <p><strong>1)</strong> tried this code for each database table: </p>
  
  <pre><code>GRANT SELECT, INSERT, UPDATE, DELETE ON  dbo.TableName TO "NT AUTHORITY\SYSTEM";
  </code></pre>
  
  <p>and without dbo.</p>
  
  <pre><code> GRANT SELECT, INSERT, UPDATE, DELETE ON  TableName TO "NT AUTHORITY\SYSTEM";
  </code></pre>
  
  <p><strong>2)</strong> I have set the permissions for this user 'NT AUTHORITY\SYSTEM' in my database:<br>
  In SSMS: Databases => myDatabase -> Security -> Users -> 'NT AUTHORITY\SYSTEM' -> Properties -> Membership -> db_owner</p>
  
  <p><strong>3)</strong> Tried to set perissions for this account in SQL Server: 
  Databases -> Security -> Logins -> 'NT AUTHORITY\SYSTEM' -> Properties -> set db_owner here</p>
  
  <p><strong>The weird thing:</strong>
  One time I have restarted the computer and everything was working!! Like this LocalSystem had permissions and the Windows Service could access the database and perform all the operations!</p>
  
  <p>But than after some circumstances this exception started to appear again. I've tried to restart the computer but failed. </p>
  
  <p>I know I'm missing something obvious. Please, help me find this thing.</p>