我在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
我已经检查了很多次并且在网上我似乎无法得到正确的答案。有什么想法吗?
答案 0 :(得分:0)
您还必须包含MySql.Data.Entity.dll
才能使其正常工作
答案 1 :(得分:0)
确保您正在连接数据库。你可以使用DatabaseContext.Exists();
如果失败,您可能需要修复连接字符串。您可以复制一些有效的服务器名称,数据库名称和密码。
然后,在保存更改之前,必须评估模型中的任何不可为空的属性是否为null。