MySql连接器6.8.2 RC,entity framework6和代码优先
我最近升级到Entity Framework 6并安装了MySql 6.8.2 RC Connector,因为它表示现在支持EF6 Code First。
请记住,在升级之前,我的项目在连接器6.7.4和EF 5中运行良好。
我对web.config进行了以下更改
我还添加了以下引用(.net 4.5版本)
- MySql.Data
- MySql.Data.Entity.EF6
- MySql.Web
问题是 – 代码调用我收到的DBContext的那一刻:
Object reference not set to an instance of an object.
再次 – 请记住,升级之前一切正常,所以我知道它不是代码问题,但是,我可能没有正确设置web.config?
另外 – 您可能会问,如果在升级之前它运行良好,为什么要升级? 好吧,据说EF6和新的MySql连接器解决了我遇到的一些错误 – 所以我希望现在实现它,而它在开发中,当它转移到生产(几个月后)我应该能够加载6.8.x连接器的生产版本。
这是堆栈跟踪,以防它有用
[NullReferenceException: Object reference not set to an instance of an object.] MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +85 System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +332 System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +67 System.Data.Entity.Infrastructure.c__DisplayClass1.b__0(Tuple`3 k) +63 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +72 System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) +260 System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +89 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +79 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +143 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +171 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +594 System.Data.Entity.Internal.InternalContext.Initialize() +31 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138 System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path) +41 System.Data.Entity.Infrastructure.DbQuery`1.Include(String path) +142 [edited].Global.Application_BeginRequest(Object sender, EventArgs e) in c:\edited\Global.asax.cs:47 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
当所有内容都在一个MVC项目中时,我有MySQL EF6和Migrations。 我把它分成几层(核心[接口/权限],数据,服务和Web)并开始得到Loren提到的相同错误。
弄清楚它没有从MVC应用程序中获取连接字符串。 事实certificate,我所要做的就是在我的Data项目中的App.config中重新创建连接字符串(DbContext和映射所在的位置)。
这些是我为使一切工作而采取的步骤:
步骤1)使用NuGet导入MySql.Data.Entities (此post的当前版本为6.8.3.0)
步骤2)将以下内容添加到App.config和/或Web.config :
步骤3)设置DbContext以使用MySql:
using MyApp.Core.Entities.Directory; using MyApp.Data.Mapping; using System.Data.Entity; namespace MyApp.Data { [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class MyContext : DbContext { public MyContext() : this("MyDB") { } public MyContext(string connStringName) : base(connStringName) {} static MyContext () { // static constructors are guaranteed to only fire once per application. // I do this here instead of App_Start so I can avoid including EF // in my MVC project (I use UnitOfWork/Repository pattern instead) DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration()); } public DbSet Countries { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // I have an abstract base EntityMap class that maps Ids for my entities. // It is used as the base for all my class mappings modelBuilder.Configurations.AddFromAssembly(typeof(EntityMap<>).Assembly); base.OnModelCreating(modelBuilder); } } }
步骤4)在Package Manager控制台中将Default Project设置为Data项目
步骤5)像往常一样使用enable-migrations
, add-migration
, update-database
ContosoUniversity MVC5 + EF6代码第一种方法使用MySQL示例下载: http ://www.nzmk.com/Blogs/BlogsView/tabid/83/EntryId/8/MVC5-EF6-ContosoUniversity-code-first-approach-using-MySQL 。 ASPX
要开始使用Entity Framework 6和Visual Studio 2013,必须为Visual Studio 1.1.1 MySQL Connector / Net 6.8.3 GA安装MySQL
可以使用以下链接下载用于Visual Studio 1.1.1 Beta的MySQL:cdn.mysql.com/Downloads/MySQLInstaller/mysql-visualstudio-plugin-1.1.1.msi
MySQL Connector / Net 6.8.3 GA dev.mysql.com/downloads/connector/net/
开始使用VS 2013和EF 6
- 为Visual Studio和Connector / Net卸载旧版本的MySQL
- 为Visual Studio 1.1.1安装MySQL
- 安装Connector / Net 6.8.3
- 将MySql.Data,MySql.Data.Entity.EF6,MySql.Web(Version 6.8.3.0)程序集的引用添加到项目中。 根据所使用的.NET Framework,可以从v4.0或v4.5文件夹中获取程序集。
-
编辑web.config或app.config
-
编辑SchoolContext.cs
....使用MySql.Data.Entity;
namespace ContosoUniversity.DAL { [DbConfigurationType(typeof(MySqlEFConfiguration))] public class SchoolContext : DbContext { .... } }
-
编辑Global.asax.cs
......使用MySql.Data.Entity;
namespace ContosoUniversity { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); DbConfiguration.SetConfiguration(new MySqlEFConfiguration()); } } }