c# - 使用 Entity Framework 访问 MySQL 数据库时出现异常
问题描述
我正在尝试让我的 .NET Framework Web 应用程序与实体框架和 MySQL 数据库一起使用
我按照本文中使用 MySql 启用实体框架的步骤:https ://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html
在我尝试之前,这一切看起来都是彩虹独角兽和阳光:
Add-Migration
在enable-migrations
包管理器控制台中执行操作。- 启动网络应用程序。
我使用以下组件:
.NET Framework 4.5.2
Entity Framework 6.2.0
MySql.Data.Entity 6.10.8
MySQL database 5.7
我收到以下错误:
添加迁移错误:
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 MySql.Data.Entity.MySqlManifestTokenResolver.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.LazyInternalContext.get_ModelBeingInitialized()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
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.
Web 应用程序数据访问错误:
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 MySql.Data.Entity.MySqlManifestTokenResolver.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.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
at System.Data.Entity.QueryableExtensions.Include[T](IQueryable`1 source, String path)
at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable`1 source, Expression`1 path)
at Jotihunt.Data.DataAccess.MapData.GetCars() in C:\SourceControl\BitBucket\Jotihunt\Data.DataAccess\MapData.cs:line 117
at Jotihunt.Business.Logic.CarFacade.GetAll() in C:\SourceControl\BitBucket\Jotihunt\Business.Logic\CarFacade.cs:line 30
at Jotihunt.Presentation.Web.Api.CarController.Get() in C:\SourceControl\BitBucket\Jotihunt\Jotihunt.Presentation.Web\Api\CarController.cs:line 21
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
我尝试了这个问题中的建议:Add-Migration not working with MySQL Unfortunatly that didn't work..
解决方案
确保以下软件具有相同的版本:
MySQL Connector/NET
MySql.Data/MySql.Data.Entity/MySql.Data.EntityFrameworks
当有人像我一样用谷歌搜索这个帖子时,这可能会有所帮助。
推荐阅读
- batch-file - 如何为我的文本文件重新排序记录?
- javafx - 升级到 Java 10 后在 CircleCI 中找不到 JavaFX
- mysql - 从表中过滤掉过时的项目
- python - 在 doxygen 中根据表达式生成公式
- subdomain - 将在端口上运行的服务映射到子域
- django - 如何在 Django 表单中显示“auto_now_add”字段?
- apache-poi - drools 无法从 XLS 文件打开决策表
- c++ - How Can I Build A Sequence Of Structs With Integer Members of Increasing Value at Compile Time in C++11
- python - 在 python27 的输入中使用变量失败并出现语法问题
- java - Java 8:RegExp - 具有各种分隔符的范围