的MySQL Connector 6.8.2 RC,实体框架6和代码优先 [英] MySql Connector 6.8.2 RC, Entity Framework 6 and Code First

查看:1293
本文介绍了的MySQL Connector 6.8.2 RC,实体框架6和代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到实体框架6和安装MySQL的6.8.2 RC连接器,它说有现在EF6的Code First支持。



保持记住,先于连接器6.7.4和EF 5的升级我的项目运作优良的运行。



我做了如下修改我的web.config

 < defaultConnectionFactory TYPE =MySql.Data.Entity.MySqlConnectionFactory,MySql.Data。 Entity.EF6/> 
<供应商>
<供应商invariantName =MySql.Data.MySqlClientTYPE =MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,版本= 6.8.2.0,文化=中性公钥= c5687fc88969c44d> ;< /提供商GT;
<供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< /供应商>





 <&是connectionStrings GT; 
<添加的providerName =MySql.Data.MySqlClientNAME =的DbContext的connectionString =服务器=本地主机;数据库=分贝; UID = UN; PWD = PW;转换零日期时间= TRUE/>
< /&是connectionStrings GT;



我还添加了以下引用(.NET 4.5版本)




  1. MySql.Data
  2. MySql.Data.Entity.EF6

  3. MySql.Web



现在的问题是 - 代码调用的DbContext的那一刻我收到:

 对象引用未设置到对象的实例。 



再 - 请记住,在升级之前一切正常,所以我知道它不是一个代码问题不过,也许我没有设置web.config中正确



另外 - 你可能会问,如果在升级前伟大的工作,为什么要升级?好了,按说EF6和新的MySQL连接器解决了一些错误,我遇到了 - 所以我希望现在执行它,而它的发展和它移动到生产(从个月了)的时候,我应该能够加载量产版的6.8.x连接器。



这里是万一堆栈跟踪它有助于

  [NullReferenceException异常:未将对象引用设置到对象的实例] 
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(连接的DbConnection)+ 85
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(连接的DbConnection)332
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices,连接的DbConnection)+67
System.Data.Entity.Infrastructure&所述;方式> c__DisplayClass1&所述; ResolveManifestToken> b__0(Tuple`3 k)的+63
System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKEY的键,Func`2 valueFactory )+72
System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(连接的DbConnection)+260
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(连接的DbConnection,DbProviderManifest&安培; providerManifest)+89
System.Data.Entity.DbModelBuilder.Build(的DbConnection providerConnection)+79
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)+143
系统。 Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)+171
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()+594
System.Data.Entity.Internal.InternalContext。初始化()+31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)+39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()+138
System.Data.Entity.Internal.Linq.InternalSet`1.Include(字符串路径)+41
System.Data.Entity.Infrastructure.DbQuery`1.Include(字符串路径)+142
〔编辑] .Global.Application_BeginRequest(对象发件人,EventArgs e)在C:\edited\Global.asax.cs:47
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute( )+136
System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔和放大器; completedSynchronously)+69


解决方案

我有MySQL的EF6和迁移了当一切都在一个MVC项目。我把它分解出来进入层(核心[接口/ Entitites],数据,服务和Web),并开始越来越罗兰提到的同样的错误。



想通了,它WASN 'T拿起从MVC应用程序的连接字符串。原来,所有我需要做的就是重新创建的App.config在我的数据项目(其中的DbContext和映射驻留)内的连接字符串。



这是我采取的步骤使一切工作正常:



步骤1)使用的NuGet导入的 MySql.Data.Entities 的(当前版本为这个职位是6.8.3.0)



第2步)添加以下的的App.config 和/或的Web.config 的:

 <&是connectionStrings GT; 
<添加名称=MYDB的providerName =MySql.Data.MySqlClient的connectionString =数据源=本地主机,端口= 3306;初始目录= mydb的; UID = myuser的; PWD =为mypass; />
< /&是connectionStrings GT;
<&的EntityFramework GT;
< defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework/>
<供应商>
<供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
<供应商invariantName =MySql.Data.MySqlClientTYPE =MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,版本= 6.8.3.0,文化=中性公钥= c5687fc88969c44d> ;< /提供商GT;
< /供应商>
< /&的EntityFramework GT;



第3步)设置你的DbContext使用MySQL:

 使用MyApp.Core.Entities.Directory; 
使用MyApp.Data.Mapping;
使用System.Data.Entity的;

命名空间MyApp.Data
{
[DbConfigurationType(typeof运算(MySql.Data.Entity.MySqlEFConfiguration))]
公共类MyContext:的DbContext
{
公共MyContext():这个(MYDB){}
公共MyContext(字符串connStringName):基地(connStringName){}
静态MyContext()
{
//静态构造函数都保证只火每个应用程序一次。
//我这样做在这里,而不是App_Start所以我能避免包括EF
//在我的MVC项目(我用的UnitOfWork / Repository模式代替)
DbConfiguration.SetConfiguration(新MySql.Data .Entity.MySqlEFConfiguration());
}

公共DbSet<国家>国家{搞定;组; }

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
//我有一个映射的ID为我的实体的抽象基类EntityMap。
//它被用作基本所有我的类映射
modelBuilder.Configurations.AddFromAssembly(typeof运算(EntityMap<>)大会。);
base.OnModelCreating(模型构建器);
}
}
}



第4步) 默认的项目设置为在Package Manager控制台的数据项目



第5步)使用启用-migrations 添加迁移更新数据库像通常那样


I recently upgraded to Entity Framework 6 and installed the MySql 6.8.2 RC Connector as it stated there was support for EF6 Code First now.

Keep in mind, prior to the upgrade my project was functioning fine running in connector 6.7.4 and EF 5.

I made the following changes to my web.config

<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

<connectionStrings>
<add providerName="MySql.Data.MySqlClient" name="dbcontext" connectionString="Server=localhost; Database=db; Uid=un; Pwd=pw;Convert Zero Datetime=True" />
</connectionStrings>

I also added the following references (.net 4.5 versions)

  1. MySql.Data
  2. MySql.Data.Entity.EF6
  3. MySql.Web

The problem is - the moment the code calls the DBContext i receive:

Object reference not set to an instance of an object.

Again - keep in mind that everything worked prior to the upgrade, so I know its not a code issue, however, maybe I didn't setup the web.config properly?

Also - you may ask, if it worked great prior to the upgrade, why upgrade? Well, supposedly EF6 and the new MySql connector solves some bugs I was running into - so I was hoping to implement it now, while its in development and by the time it moves to production (months from now) I should be able to load in the production version of the 6.8.x connector.

Here is the stack trace in case it helps

[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.<ResolveManifestToken>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

解决方案

I had MySQL EF6 and Migrations up and working when everything was in one MVC project. I split it out into layers (Core[Interfaces/Entitites], Data, Services, and Web) and started getting the same error Loren mentioned.

Figured out that it wasn't picking up the connection string from the MVC app. Turns out that all I had to do was re-create the connection string within the App.config in my Data project (where the DbContext and mappings reside).

These are the steps I took to get everything working:

Step 1) Use NuGet to import MySql.Data.Entities (current version as of this post is 6.8.3.0)

Step 2) Add the following to App.config and/or Web.config:

<connectionStrings>
    <add name="MyDB" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306; Initial Catalog=mydb; uid=myuser; pwd=mypass;" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>

Step 3) Set up your DbContext to use 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<Country> 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);
        }
    }
}

Step 4) Set the Default Project to your Data project in the Package Manager Console

Step 5) Use enable-migrations, add-migration, update-database like you normally would

这篇关于的MySQL Connector 6.8.2 RC,实体框架6和代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆