简体   繁体   中英

Add-Migration not working with MySQL

I have downloaded Asp.Net MVC 5.x Single page web application from https://as.netboilerplate.com/Templates . I'm using MySQL and followed the steps mentioned in link https://as.netboilerplate.com/Pages/Documents/EF-MySql-Integration . But when I run the Add-Migration command it gives the error.

MyCompany.MyProject.Web\Web.Config:

<add name="Default" connectionString="Server=127.0.0.1;port=3306;Database=SparTestDb;uid=root;password=root" providerName="MySql.Data.MySqlClient"/>

MyCompany.MyProject.EntityFramework\Migrations\Configuration.cs

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    ContextKey = "Spar";

    SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
}

Error:

PM> Add-Migration "AbpZero_Initial" System.NullReferenceException: Object reference not set to an instance of an object. at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.b__0(Tuple 3 k) at System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory) at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy 2 valueFactory) at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext() at Abp.EntityFramework.AbpDbContext.RegisterToChanges() at Abp.Zero.EntityFramework.AbpZeroDbContext 3..ctor(String nameOrConnectionString) at MyCompany.MyProject.EntityFramework.SparDbContext..ctor() in C:\Users\MyName\Downloads\MyCompany.MyProject (1)\3.3.0\src\MyCompany.MyProject.EntityFramework\EntityFramework\SparDbContext.cs:line 19 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance() at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func 1 resolver) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration) at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() Object reference not set to an instance of an object. PM>

MySql.Data version 6.9.11 is working well. Higher versions have problems. So, Try to use nuget.org/packages/MySql.Data/6.9.11 . Or if you are using EF Core, you can use Npgsql.EntityFrameworkCore.PostgreSQL

I have MySql.Data of version 8.0.26 and MySql.Data.Entitiframework 8.0.26, after debugging to sourcecodes i've found that for local migrations EF tryes to connect using SSL, so in my case helped to turn OFF ssl using connection strin parameter:

SslMode=none

Example connection string:

"server=localhost;port=3306;user id=root;password=1q2w][=[;database=TmpMigrations;persistsecurityinfo=True;Allow User Variables=True;SslMode=none;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM