gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:56:02 26 4
gpt4 key购买 nike

我对 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com