標籤:
項目中有使用MySql資料庫,EF做資料層,發現有一張表儲存時始終報錯。報錯資訊如下
System.NullReferenceException: 未將對象引用設定到對象的執行個體。
在 MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
在 MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
在 MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
在 MySql.Data.Entity.SqlFragment.ToString()
在 MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
在 MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
在 System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
在 System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
在 System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
在 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
在 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2identifierValues,List`1 generatedValues)
在 System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
在 System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
在 System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
在 System.Data.Entity.Internal.InternalContext.SaveChanges()
在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
在 System.Data.Entity.DbContext.SaveChanges()
後來在StackOverFlow上得到提示,發現是MySql Connector .Net 6.6.5這個版本的問題,在這個版本中,如果表中的主鍵是自增長的,當該列的類型是TinyInt、UTinyInt、UBigInt時,會出現以上錯誤。但是沒有得到解決方案。
不過無需太大驚慌,在之前的版本沒有發現這個問題,然後去官網看了一下,最新的MySql Connector .Net已經是6.9.5,經測試也沒有這個問題。
MySql to EF在SaveChange時報Null 參考的錯誤