从 Windows 服务首先调用 EF 代码

Calling EF Code First from Windows Service

我正在尝试在 Windows service.But 的 OnStart() 事件中做一个简单的 select 系统,一旦我引入与数据库到 OnStart() 事件我收到以下错误:

An exception occurred while initializing the database. See the InnerException for details. The underlying provider failed on Open.

C#代码:

protected override void OnStart(string[] args)
{
    try
    {
        string serverName = GetServerName();
        string serverIp = GetServerIP();

        //This is the line that causes the exception
        var dbContext = new DatabaseContext();

        bool serverExists = dbContext.Server.Any(s => s.ServerName == serverName && s.ServerIp == serverIp);

        if (!serverExists)
        {
            //Add server details to the DB
        }
    }
    catch (Exception eX)
    {
        using (var w = new StreamWriter("C:\ErrorFile.txt", true))
        {
            w.WriteLine(DateTime.Now.ToString("dd MM yyyy HH:mm:ss"));
            w.WriteLine(eX.Message);
            if (eX.InnerException != null)
                w.WriteLine(eX.InnerException.Message);
            w.WriteLine(eX.StackTrace);
            w.WriteLine();
            w.Close();
        }
    }
}

App.config:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="ServiceManagerConnection" providerName="System.Data.SqlClient" connectionString="Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=ServiceManager;Data Source=." />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="ServiceRunInterval" value="5" />
  </appSettings>
</configuration>

堆栈跟踪:

  at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
   at mySuperSecretProjectName.Program.OnStart(String[] args) in c:\mySuperSecretProjectName\Program.cs:line 49

问题出在我的连接 string.All 我必须做的是更改连接字符串以使用 SQL 用户名和密码凭据,因为 Windows 我创建的服务本地系统权限无权与 SQL

交谈