7

I am completely new to Entity Framework and even ADO.NET in general (don't typically do much work with databases).

  1. I downloaded and installed MySQL Connector/NET 6.3.5.
  2. I created a new C# project in Visual Studio 2010.
  3. I added a new ADO.NET Entity Data Model to my project and chose "Generate from database."
  4. I added a new connection to my local MySQL server w/ server name "localhost" + my user name and password.
  5. I checked all of the tables from my MySQL database to generate objects for.
  6. I wrote the following code:

(things is just a bogus table I threw together with some arbitrary fields.)

TestDataEntities entities = new TestDataEntities();

var things = entities.things.Execute(MergeOption.AppendOnly); // exception

The above threw a NullReferenceException and I'm really clueless as to why that could be. When I test the connection, it says it succeeded. I have definitely set up the connection with the correct user name and password. I don't even know what else to investigate.

For those of you who actually know about this stuff, here's the exception I got (though, to my untrained eyes, this doesn't actually look like it would be all that helpful):

   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.Metadata.Edm.StoreItemCollection.Loader.InitializeProviderManifest(Action`3 addError)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.OnProviderManifestTokenNotification(String token, Action`3 addError)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleProviderManifestTokenAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.SchemaElement.ParseAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.SchemaElement.Parse(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleTopLevelSchemaElement(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.InternalParse(XmlReader sourceReader, String sourceLocation)
   at System.Data.EntityModel.SchemaObjectModel.Schema.Parse(XmlReader sourceReader, String sourceLocation)
   at System.Data.EntityModel.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList`1& schemaCollection)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths)
   at System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, Memoizer`2& cachedCTypeFunction)
   at System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths)
   at System.Data.Metadata.Edm.MetadataCache.StoreMetadataEntry.LoadStoreCollection(EdmItemCollection edmItemCollection, MetadataArtifactLoader loader)
   at System.Data.Metadata.Edm.MetadataCache.StoreItemCollectionLoader.LoadItemCollection(StoreMetadataEntry entry)
   at System.Data.Metadata.Edm.MetadataCache.LoadItemCollection[T](IItemCollectionLoader`1 itemCollectionLoader, T entry)
   at System.Data.Metadata.Edm.MetadataCache.GetOrCreateStoreAndMappingItemCollections(String cacheKey, MetadataArtifactLoader loader, EdmItemCollection edmItemCollection, Object& entryToken)
   at System.Data.EntityClient.EntityConnection.LoadStoreItemCollections(MetadataWorkspace workspace, DbConnection storeConnection, DbProviderFactory factory, DbConnectionOptions connectionOptions, EdmItemCollection edmItemCollection, MetadataArtifactLoader artifactLoader)
   at System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections)
   at System.Data.EntityClient.EntityConnection.InitializeMetadata(DbConnection newConnection, DbConnection originalConnection, Boolean closeOriginalConnectionOnFailure)
   at System.Data.EntityClient.EntityConnection.Open()
   at System.Data.Objects.ObjectContext.EnsureConnection()
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)
   at EntityFrameworkTest.Form1..ctor() in D:\Development\EntityFrameworkTest\Form1.cs:line 23
   at EntityFrameworkTest.Program.Main() in D:\Development\EntityFrameworkTest\Program.cs:line 18
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Ralph also posted the question here: http://forums.mysql.com/read.php?47,391001,391001 – Rami A. Oct 30 '10 at 04:13
  • And the answer that solved it for me was to copy the MySql.Data.Entity.dll to the bin directory for my hosted app. – sipsorcery Dec 08 '10 at 09:37
  • I think this stack-exchange [proposal](http://area51.stackexchange.com/proposals/11464/code-review?referrer=aWNm_PdciyFqjFW8CUacGw2 "code review") might be of interest to you. If it is show your support and help get it into beta so you can start poking fun at other people's code! :) – greatwolf Jan 16 '11 at 23:11

5 Answers5

14

I got past this error by adding a reference to MySql.Data.Entity.dll (and MySql.Web.dll)

Rami A.
  • 10,302
  • 4
  • 44
  • 87
2

That's a provider bug. It may be a bug which is triggered by a configuration error, but it is a provider bug nonetheless. You should talk to the people who wrote your MySQL provider, and send them that stack and test case.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
2

Good news ! This issue has been solved in MySQL Connector/NET 6.3.6 Update your MySql Connector to solved the problem.

DerekX6u
  • 21
  • 1
1

Well, I don't know if this is the way it's supposed to be or not (if it is, I'm a little embarrassed -- though in my defense I fail to see anywhere this is clearly indicated); but it seems the problem was that I had my target framework set to .NET 3.5. After changing my project's target framework (honestly, on a whim) to .NET 4.0, the NullReferenceException went away.

So, as unsatisfying as it may be, that seems to have been the solution in my case.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 1
    I get `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) ...` if I upgrade to MySql.Data.EF6.dll and forget to target .NET 4. – Rami A. Mar 25 '16 at 06:14
  • @agDev, change your project to target .NET 4. – Rami A. Aug 02 '18 at 00:22
1

In the MySqlClientFactory-> MySqlDbProviderServicesInstance property. It reference the ass:MySql.Data.Entity use the code

string str = Assembly.GetExecutingAssembly().FullName.Replace("MySql.Data", "MySql.Data.Entity");

But in the MySql Client 6.3.5 the MySqlData's version is 6.3.5, and MySql.Data.Entity's version is 6.3.4(in \Program Files\MySQL\MySQL Connector Net 6.3.5\Assemblies\v2.0).so it can't load it successfully.

In the folder(\Program Files\MySQL\MySQL Connector Net 6.3.5\Assemblies\v4.0) ,the MySql.Data.Entity's version is 6.3.5,so itcan load successed.

So the one way is change the target to .NET 4.0,and another is roll the MySql.Data back to 6.3.2/6.1.2 .

chsword
  • 2,032
  • 17
  • 24