Microsoft.EntityFrameworkCore MySql空引用异常。 ASP.Net核心1.1

时间:2018-08-20 10:56:38

标签: c# asp.net .net entity-framework

我在asp.net核心上有一个数据库和一个应用程序,当我在添加迁移时将数据库和应用程序移动到另一台计算机时,遇到了以下问题:

Add-Migration InitialCreate -context MySqlDbContext
System.NullReferenceException: Object reference not set to an instance of an object.
   at MySql.Data.MySqlClient.MySqlConnection.CloseDatabase()
   at MySql.Data.MySqlClient.MySqlConnection.DoClose()
   at MySql.Data.MySqlClient.MySqlConnection.Dispose(Boolean disposing)
   at System.Data.Common.DbConnection.Dispose()
   at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlConnectionSettings.<>c__DisplayClass2_0.<GetSettings>b__0(String key)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlScopedTypeMapper.get_ConnectionSettings()
   at Microsoft.EntityFrameworkCore.Storage.Internal.MySqlScopedTypeMapper.MaybeConvertMapping(RelationalTypeMapping mapping)
   at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapperExtensions.IsTypeMapped(IRelationalTypeMapper typeMapper, Type clrType)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType, Boolean runConventions)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Boolean runConventions)
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(Type type)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.RelationalModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

我如何连接到数据库:

public class MySqlDbContext : DbContext
{
    public static DbContextOptions<MySqlDbContext> Options;

    public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
    {
        Options = options;
    }

    [ThreadStatic]
    protected static MySqlDbContext current;

    public static MySqlDbContext Current()
    {
        if (current == null)
            current = new MySqlDbContext(Options);

        return current;
    }

    ...
}



public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MySqlDbContext>(
       options => options.UseMySql(mysqlConnection)
    );
}

  "ConnectionStrings": {
"MiddleWareMySqlProvider": "server=localhost;port=3306;Uid=root;password=root;Database=idapdb2;persistsecurityinfo=True; Pooling=false; convert zero datetime=True"
  },

我以为问题出在连接字符串上,但是在其他应用程序中一切正常。

我不知道如何解决这个问题。

0 个答案:

没有答案