4

I am absolutely new to asp.net core development. I am trying to create a simple asp.net core web api with a single model and mysql to store the model data and then I would want to retrieve it as REST API perhaps using Swagger.

My current set up looks like following:

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

And in my startup.cs:

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

            services.AddMvc();
        }

After this much of set up, I restored all the packages and executed dotnet ef migrations add initialwhich created Migrations folder (I guess it means it was successful). After that I executed dotnet ef database update which is giving me error: Format of the initialization string does not conform to specification starting at index 0

What is goin wrong?

1 Answer 1

4

Change your ConfigureServices methode code with:

services.AddDbContext<WebAPIDataContext>(options =>
    options.UseMySQL(Configuration.GetConnectionString("SampleConnection"))
);
  • Configuration.GetConnectionString("SampleConnection") returns the connection string server=localhost;userid=root;pwd=root;port=3306;database=asptest;sslmode=none; you set in appsettings.json,
  • whereas options.UseMySQL("SampleConnection") would try to interpret literaly SampleConnection as a connection string.
Sign up to request clarification or add additional context in comments.

7 Comments

I just tried this but it gave me System.NotImplementedException: The method or operation is not implemented. at MySQL.Data.EntityFrameworkCore.Migrations.MySQLHistoryRepository.get_ExistsSql() error.
@Nitish Now your configuration connection is good. You have now a problem with Migration. I suppose the problem comes from the initial migration you created. Try to reset your migrations with dotnet ef migrations remove initial and then recreate-update the schema.
So for safer side I simply deleted the Migrations folder and in my package manager console did: Add-Migration Initial > Migrations folder was again created > then I ran Database-Update -Context WebAPIDataContext and I got the same error again. I saw this post: stackoverflow.com/questions/39640072/… Is it helpful in my case?
However, a database is already created with a table _efmigrationshistory
@Nitish Unfortunately the problem described is yours. The only things you can do are 1. check you have version 7.0.6-IR31 otherwise 2. you can use another package SapientGuardian.EntityFrameworkCore.MySql (the advice of Careuno Merchan in your link). If none of that works, you will have to wait for a stable release. Depending on your needs, you might want to switch your database and use SQL Server 2016 or SQL Local DB. You can install them for free on your dev computer and so far I had no problem with .NET Core.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.