Cannot connect to MySql using Repository pattern
Posted by: Silvi Alam
Date: September 10, 2013 11:58PM

Hi
We have got a project with multiple .Net solutions (C#) and every solution is implemented using the Repository and Unit of Work pattern. We are also using Entity Framework 5.0 as the data access technology and MS SQL as the database server. All the solutions are in visual studio 2012. Now we want migrate to MySQl server as our database server without doing much change in the design. To do this what I have done is
1. Migrated all the tables and data to MySql from Ms SQL. (We are not using any stored procedures).
2. I have installed the ADO.NET driver for MySQL which is Connector/Net 6.7.4.
3. I have added a reference to the MySql.Data.dll in the Data layer project and test project.
But every time I am trying getting the DBContext from the database I am receiving the following error:
Object reference not set to an instance of an object. at MySql.Data.MySqlClient.MySqlClientFactory.get_MySqlDbProviderServicesInstance() at MySql.Data.MySqlClient.MySqlClientFactory.System.IServiceProvider.GetService(Type serviceType) at System.Data.Common.DbProviderServices.GetProviderServices(DbProviderFactory factory) at System.Data.Entity.ModelConfiguration.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.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet`1.get_Local() at System.Data.Entity.DbSet`1.get_Local()"
I have also tried to add a reference MySql.Data.Entity.dll in the Dal layer project I am receiving the following error when trying to access the DBContext
“Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.”
I have tried to add the MySql.Web.dll and same error. And then I am not using MySql.Web.dll anymore because I could not find any reason using this anyway.
For both the dls (MySql.Data.Entity.dlland MySql.Data.dll) the Copy local property was set true. And I have checked the version of the dlls. Both of them are version 6.7.4.0. The config file had the following sections added:
<system.data>
<DbProviderFactories>
<clear />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description="Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,Version=6.7.4.0, Culture=neutral,PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>

<connectionStrings>
<add name="MyDbContextName" connectionString="SERVER=aaa.bbb.ccc.ddd; DATABASE= MyDbContextDbName; UID=xxx; PASSWORD=xxxxx;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

To resolve the issue I have tried the following options:
1. Removed MySql.Data.Entity.dll and MySql.Data.dll from the data layer projects, got the same dlls using the Nuget Package, and cleaned the solution but the same behaviour as above.
2. I have applied the steps mentioned in the following link http://www.jeffscorner.org/blog/category/MySQL.aspx. But the same result.
3. I have applied the steps mentioned in the following link http://stackoverflow.com/questions/14582751/mysql-connector-always-get-object-reference-not-set-to-an-instance-of-an-object. But the same result.
Our code is using the following pattern
public class MyBaseContext<T> : DbContext where T : DbContext
{

static MyBaseContext ()
{
Database.SetInitializer<T>(null);
}

protected MyBaseContext ()
: base("name=MyDbContextName")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<MyEntity>().ToTable("MyEntity");
}

}

[Table("MyEntity")]
public class MyEntity
{
[Key]
public string Id { get; set; }

[ConcurrencyCheck]
public int NextSeed { get; set; }
}

public class MyContext : MyBaseContext <MyContext>
{
public DbSet<MyEntity> MyEntities { get; set; }
}

public interface IMyRepository
{
MyEntity FindById(int Id);
void Commit();
}
public class MyRepository : IMyRepository
{
MyContext context{get;set;}

public MyRepository ()
{
context = new MyContext ();
}

#region IMyRepository Members

public MyEntity FindById (int Id)
{
return context.MyEntities
.FirstOrDefault(x => x.Id == Id);
}

public void Commit()
{
context.SaveChanges();
}

#endregion
}

Any kind of help would be much appreciated.
Thanks & Regards,
Silvi

Options: ReplyQuote


Subject
Written By
Posted
Cannot connect to MySql using Repository pattern
September 10, 2013 11:58PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.