2017-02-25 7 views
2

Ich bin absolut neu in der Kernentwicklung asp.net. Ich versuche, eine einfache asp.net Core Web API mit einem einzigen Modell und MySQL zu erstellen, um die Modelldaten zu speichern, und dann möchte ich es als REST API möglicherweise mit Swagger abrufen.asp.net Kern/EF-Core Mysql Update Datenbank Fehler

Mein aktueller Set up sieht wie folgt aus:

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; } 
    } 

} 

Project.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" 
    } 
    } 
} 

Und in meinem Startup.cs:

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

      services.AddMvc(); 
     } 

Nach diesem viel aufgebaut, restauriert ich alle Pakete und ausgeführt dotnet ef migrations add initial die Migrations-Ordner erstellt (ich denke, es bedeutet, dass es erfolgreich war). Danach habe ich dotnet ef database update ausgeführt, was mir einen Fehler gibt: Format of the initialization string does not conform to specification starting at index 0

Was ist los falsch?

Antwort

3

ändern ConfigureServices Methode Code mit:

services.AddDbContext<WebAPIDataContext>(options => 
    options.UseMySQL(Configuration.GetConnectionString("SampleConnection")) 
); 
  • Configuration.GetConnectionString("SampleConnection") gibt die Verbindungszeichenfolge server = localhost; Benutzer-ID = root; PWD = Wurzel; port = 3306; database = asptest; sslmode = none; setzen Sie in appsettings.json,
  • , während options.UseMySQL("SampleConnection") versuchen würde, literal SampleConnection als eine Verbindungszeichenfolge zu interpretieren.
+0

Ich habe gerade versucht, aber es gab mir 'System.NotImplementedException: Die Methode oder Operation ist nicht implementiert. bei MySQL.Data.EntityFrameworkCore.Migrations.MySQLHistoryRepository.get_ExistsSql() 'Fehler. – Nitish

+0

@Nitish Jetzt ist Ihre Konfigurationsverbindung gut. Sie haben jetzt ein Problem mit der Migration. Ich nehme an, dass das Problem von der _initial_ Migration kommt, die Sie erstellten. Versuchen Sie, Ihre Migrationen mit 'dotnet ef migrations remove initial' zurückzusetzen und dann das Schema neu zu erstellen. –

+0

Also für sicherere Seite Ich löschte einfach die Migrations-Ordner und in meiner Paket-Manager-Konsole tat: 'Add-Migration Initial'> Migrations-Ordner wurde erneut erstellt> dann habe ich' Datenbank-Update -Context WebAPIDataContext' ausgeführt und ich habe den gleichen Fehler erneut . Ich habe diesen Beitrag gesehen: http://stackoverflow.com/questions/39640072/dot-net-entity-framework-database-update-doesnt-create-tables-in-mysql-database Ist es in meinem Fall hilfreich? – Nitish