asp.net-core - asp.net core/EF-Core mysql更新数据库错误

标签 asp.net-core asp.net-core-mvc entity-framework-core

我对 ASP.NET Core 开发完全陌生。我正在尝试使用单个模型和 mysql 创建一个简单的 asp.net core Web api 来存储模型数据,然后我想使用 Swagger 将其作为 REST API 检索。

我当前的设置如下所示:

Books.cs:

using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace New_Api.Models
{
    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public decimal Price { get; set; }
        public DateTime CreatedOn { get; set; } = DateTime.UtcNow;

    }

    public class WebAPIDataContext : DbContext
    {
        public WebAPIDataContext(DbContextOptions<WebAPIDataContext> options)
            : base(options)
        {
        }
        public DbSet<Book> Books { get; set; }
    }

}

项目.json:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0",
    "MySql.Data.Core": "7.0.4-IR-191",
    "MySql.Data.EntityFrameworkCore": "7.0.4-IR-191",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

appsettings.json:

{

    "ConnectionStrings": {
      "SampleConnection": "server=localhost;userid=root;pwd=root;port=3306;database=asptest;sslmode=none;"
    },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

在我的startup.cs中:

 public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddDbContext<WebAPIDataContext>(options =>
            {
                options.UseMySQL("SampleConnection");
            });

            services.AddMvc();
        }

经过这么多设置后,我恢复了所有包并执行了 dotnet ef migrations add initial,它创建了 Migrations 文件夹(我想这意味着它成功了)。之后,我执行了 dotnet ef database update ,这给了我错误:初始化字符串的格式不符合从索引 0 开始的规范

出了什么问题?

最佳答案

更改您的ConfigureServices方法代码:

services.AddDbContext<WebAPIDataContext>(options =>
    options.UseMySQL(Configuration.GetConnectionString("SampleConnection"))
);
  • Configuration.GetConnectionString("SampleConnection") 返回连接字符串server=localhost;userid=root;pwd=root;port=3306;database=asptest;sslmode=none; 您在 appsettings.json 中设置,
  • options.UseMySQL("SampleConnection") 会尝试将 SampleConnection 字面解释为连接字符串。

关于asp.net-core - asp.net core/EF-Core mysql更新数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42460997/

相关文章:

asp.net-core - 在重定向到客户端之前,如何在身份服务器中获取 id_token?

c# - 具有范围的 Asp.net 核心 2.1 OpenIdConnectOptions 不起作用

datatable - Asp Core 1.1、DataTable两者都存在

c# - EF Core 2.1 [更改跟踪] - 保存所有相关实体

c# - Entity Framework Core 自引用可选属性

entity-framework - 使用 F# 配置 EF Core

c# - asp.net core 1 appsettings.production.json 不更新连接字符串

c# - ASP.Net MVC 6 路由错误

c# - ASP.NET 5 类库中的 Startup.cs

.net - 如何在 ASP.NET 启动时重试运行 EFCore Migrate?