【问题标题】:Deploying ASP.net Core 2.0 to windows server将 ASP.net Core 2.0 部署到 Windows 服务器
【发布时间】:2018-06-30 16:23:16
【问题描述】:

我已将 asp.net core 2.0 应用程序部署到安装了 MsSQL 2017 的 Windows Server 2012。当我请求数据库相关方法时出现以下错误。

appsettings.json 内容为:

 {
    "ConnectionStrings": {
        "DefaultConnection": "Server=MGSERVER2012\SQLEXPRESS;Database=TestingCoreIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

错误详情:

【问题讨论】:

  • 您是否在 startup.cs 中为ConnectionString 注册服务配置?你是如何实例化数据库连接的。
  • 是的,我正在这样做,该应用程序在 Visual Studio 2017 的本地计算机上运行良好。这是我在 configureServices 中的代码: services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString ("DefaultConnection")));

标签: asp.net sql-server asp.net-core .net-core iis-8


【解决方案1】:

所以我在我的应用程序中做了什么(CORE 1.0,在 2.0 中应该类似)

在配置中注册 -

  public void ConfigureServices(IServiceCollection services)
        {
            #region connection
            services.AddDbContext<TWCStoreContext>(options => options.UseSqlServer(Configuration.GetValue<string>("Data:DefaultConnection")));
            #endregion
            services.Configure<ConfigData>(Configuration.GetSection("Data"));
         }

在上下文文件中 -

public partial class TWCStoreContext : DbContext
    {
        private readonly IOptions<ConfigData> _ConfigData;

        public TWCStoreContext(DbContextOptions<TWCStoreContext> options, IOptions<ConfigData> ConfigData)
       : base(options)
        {
            _ConfigData = ConfigData;
        }

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(_ConfigData.Value.DefaultConnection);
        }

    }

配置 -

public class ConfigData
    {
        public string DefaultConnection{ get; set; }
    }

确保你没有遗漏任何东西。

【讨论】:

    【解决方案2】:

    需要在 Startup.cs 的 ConfigureServices 方法中指定连接字符串名称,如下所示。将您的应用程序 DBContext 替换为 "YourDBContext"

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<YourDbContext>(op => p.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    
        }
    

    【讨论】:

      【解决方案3】:

      我找到了我的问题的答案: 我在管理工作室中创建了具有完全权限的新用户,并在 appsettings.json 中使用了这个连接字符串

       "DefaultConnection": "Server=MGSERVER2012\\SQLEXPRESS;Database=xxx;MultipleActiveResultSets=true;Persist Security Info=True;User ID=AspCoreUser;Password=Password@123" 
      

      我还为 appsettings.json 的用户组授予了 Windows 权限。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        • 2020-07-08
        • 1970-01-01
        相关资源
        最近更新 更多