首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对数据库上下文执行NullReferenceException时发生SaveChanges错误

对数据库上下文执行NullReferenceException时发生SaveChanges错误
EN

Stack Overflow用户
提问于 2013-07-26 18:47:18
回答 2查看 701关注 0票数 1

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

代码语言:javascript
复制
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();

以下是捕获异常的堆栈跟踪:

代码语言:javascript
复制
   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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-11 12:21:58

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

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

票数 0
EN

Stack Overflow用户

发布于 2013-10-18 07:54:47

您还必须包括MySql.Data.Entity.dll才能使其工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17888800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档