首页 文章

将ASP.net Core 2.0部署到Windows服务器

提问于
浏览
0

我已经将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"
    }
  }
}

错误详情:
enter image description here

3 回答

  • 0

    所以我在我的应用程序中做了什么(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; }
        }
    

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

  • 0

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

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

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

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

    我还为appsettings.json的Users组授予了windows权限 .

相关问题