1

我已经在 MySQL 中设置了我的表,在服务器资源管理器中添加了连接,添加了正确的程序集,创建了正确的 ADO.NET 实体模型并确保了正确的 app.config 文件。当我想存储到数据库时,我正在运行此代码:

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();

这是捕获的异常的堆栈跟踪:

   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

我查了很多次,网上我似乎无法得到正确的答案。有什么想法吗?

4

2 回答 2

0

你还必须包括MySql.Data.Entity.dll它才能工作

于 2013-10-18T07:54:47.997 回答
0

确保您正在连接数据库。您可以使用DatabaseContext.Exists(); 如果失败,您可能必须修复连接字符串。您可以复制一些工作的并更改服务器名称、数据库名称和密码。

然后,您必须在保存更改之前评估模型中的任何不可为空的属性是否为空。

于 2014-02-11T12:21:58.107 回答