1

I have set up my table in MySQL, added the connection in the Server Explorer, added the correct assemblies, created the proper ADO.NET entity model and ensured correct app.config file. I am running this code when I want to store to the database:

MySQLEntity MySQLDB= new MySQLEntity();
mysqlspectra DBSPectra = new mysqlspectra();
DBSPectra.DateTime = DateTime.Now;
DBSPectra.Name = null;
DBSPectra.Version = spectra.Info.Version;
DBSPectra.SerialHighNumber = spectra.Info.SerialHighNumber;
DBSPectra.SerialLowNumber = spectra.Info.SerialLowNumber;
DBSPectra.Completed = spectra.Info.Completed;
DBSPectra.SpectrometerID = spectra.Info.SpectrometerID;
DBSPectra.GasCellID = spectra.Info.GasCellID;
DBSPectra.Format = (short)spectra.Info.Format;
DBSPectra.Apodization = (short)spectra.Info.Apodization;
DBSPectra.PhaseApodization = (short)spectra.Info.PhaseApodization;
DBSPectra.Temperature = spectra.Info.Temperature;
DBSPectra.Pressure = spectra.Info.Pressure;
DBSPectra.NumScans = spectra.Info.NumScans;
DBSPectra.Resolution = spectra.Info.Resolution;
DBSPectra.Gain = spectra.Info.Gain;
DBSPectra.PathLength = spectra.Info.PathLength;
DBSPectra.FirstPoint = spectra.Info.FirstPoint;
DBSPectra.LastPoint = spectra.Info.LastPoint;
DBSPectra.MaxFrequency = spectra.Info.MaxFrequency;
DBSPectra.MaxLocPoint = spectra.Info.MaxLocPoint;
DBSPectra.MinLocPoint = spectra.Info.MinLocPoint;
DBSPectra.NumDataPoints = spectra.Info.NumDataPoints;
DBSPectra.NumDataPhase = spectra.Info.NumDataPhase;
DBSPectra.Step = spectra.Info.Step;
DBSPectra.IgramType = (short)spectra.Info.IgramType;
DBSPectra.DataPoints = GetBytes(spectra.DataPoints);
MySQLDB.mysqlspectras.AddObject(DBSPectra);
MySQLDB.SaveChanges();

Here is the stacktrace from the caught exception:

   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(Action3 addError)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.OnProviderManifestTokenNotification(String token, Action3 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(IEnumerable1 xmlReaders, IEnumerable1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList1& schemaCollection)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable1 xmlReaders, IEnumerable1 sourceFilePaths)
   at System.Data.Metadata.Edm.StoreItemCollection.Init(IEnumerable1 xmlReaders, IEnumerable1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction)
   at System.Data.Metadata.Edm.StoreItemCollection..ctor(IEnumerable1 xmlReaders, IEnumerable1 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](IItemCollectionLoader1 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.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Objects.ObjectContext.SaveChanges()
   at Database_Test.Form1.saveButton_Click(Object sender, EventArgs e) in C:\Users\Jorge\Documents\Visual Studio 2010\Projects\Database_Test\Database_Test\Form1.cs:line 159

I have checked so many times and online I can't seem to get the correct answer. Any thoughts?

ekad
  • 14,436
  • 26
  • 44
  • 46
Corey Berigan
  • 614
  • 4
  • 14
  • 4
    Most likely `spectra` or one of its properties that you are referencing is null. – Khan Jul 26 '13 at 18:49
  • I have checked this already. All values assigned are not null. – Corey Berigan Jul 26 '13 at 18:54
  • `MySQLDB.mysqlspectras.Add(DBSPectra);` – Amin Saqi Jul 26 '13 at 19:00
  • Your connection string is wrong, something is missing or MySQL driver dependency is missing. Your code is right. – Akash Kava Jul 26 '13 at 19:08
  • @AminSaghi That is not possible. The mysqlspectras is an ObjectSet of EntityObject. AddObject() is the only function provided pertaining to adding an object. – Corey Berigan Jul 26 '13 at 19:09
  • @AkashKava What do you mean driver dependency? Like something in MySql.Data? Something that should be provided by the MySql Connector? – Corey Berigan Jul 26 '13 at 19:17
  • Is there a separate file for MySQL Entity dll? I am sure your connection string is missing provider name or it is not correct. what is provider name in your connection string? – Akash Kava Jul 26 '13 at 19:20
  • This is what EF generated. The connection string is in there. `` – Corey Berigan Jul 26 '13 at 19:22
  • http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net has some good suggestions. – Dour High Arch Jul 26 '13 at 19:53
  • @DourHighArch Yeah, I understand what this means, but I just don't understand how I am getting this exception considering that the entire set-up was automatically done by the IDE. I set up the database connection with the IDE, the entity model, and created the entity. Now, it says it's not being instantiated. – Corey Berigan Jul 29 '13 at 18:44

2 Answers2

0

You have to also include MySql.Data.Entity.dll for it to work

Mord Zuber
  • 298
  • 10
  • 19
0

Make sure you are connecting with the database. You can use DatabaseContext.Exists(); If it fails, you'll probably have to fix your connection string. You can copy some working one and chage the server name, the database name and password.

Then you have to evaluate if any of the not nullable properties in your model are null before you save changes.

Alejandro del Río
  • 3,966
  • 3
  • 33
  • 31