MySQL with Entity Framework - what am I doing wrong?
I am completely new to Entity Framework and even ADO.NET in general (don't typically do much work with databases).
- I downloaded and installed MySQL Connector/NET 6.3.5.
- I created a new C# project in开发者_运维问答 Visual Studio 2010.
- I added a new ADO.NET Entity Data Model to my project and chose "Generate from database."
- I added a new connection to my local MySQL server w/ server name "localhost" + my user name and password.
- I checked all of the tables from my MySQL database to generate objects for.
- 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()
I got past this error by adding a reference to MySql.Data.Entity.dll (and MySql.Web.dll)
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.
Good news ! This issue has been solved in MySQL Connector/NET 6.3.6 Update your MySql Connector to solved the problem.
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.
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 .
精彩评论