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)
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
MySQL Connector/NET is compatible with multiple versions of Entity Framework Core. For specific compatibility information, see Table 7.2, “Connector/NET Versions and Entity Framework Core Support”.
Entity Framework is the name given to a set of technologies that support the development of data-oriented software applications. MySQL Connector/NET supports Entity Framework 6.0 (EF6 or EF 6.4) and Entity Framework Core (EF Core), which is the most recent framework available to .
MySQL Connector/NET enables you to develop . NET applications that require secure, high-performance data connectivity with MySQL. It implements the required ADO.NET interfaces and integrates into ADO. NET-aware tools. You can build applications using your choice of .
The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.
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
ContosoUniversity MVC5 + EF6 code first approach using MySQL example download: http://www.nzmk.com/Blogs/BlogsView/tabid/83/EntryId/8/MVC5-EF6-ContosoUniversity-code-first-approach-using-MySQL.aspx
To start using Entity Framework 6 and Visual Studio 2013 it is necessary to install MySQL for Visual Studio 1.1.1 MySQL Connector/Net 6.8.3 GA
MySQL for Visual Studio 1.1.1 Beta can be downloaded with the following link: 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/
To start working with VS 2013 and EF 6
Edit web.config or app.config
<connectionStrings> <add name='SchoolContext' connectionString='Data Source=xxxxx;port=3306;Initial Catalog=ContosoUniversity;user id=root;password=xxxxxxxxx;' providerName='MySql.Data.MySqlClient'/> </connectionStrings> <entityFramework codeConfigurationType='MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6'> <defaultConnectionFactory type='System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework'/> <providers> <provider invariantName='MySql.Data.MySqlClient' type='MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.Entity.EF6' />
Edit SchoolContext.cs
.... using MySql.Data.Entity;
namespace ContosoUniversity.DAL { [DbConfigurationType(typeof(MySqlEFConfiguration))] public class SchoolContext : DbContext { .... } }
Edit Global.asax.cs
...... using 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()); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With