當我嘗試插入相同的數據時,我得到NullReferenceException異常。實體框架6與Mysql和NullReferenceException
我已經閱讀了Stackoverflow,asp.net論壇和mysql社區論壇中的大量帖子。 我找不到這個問題的任何真正的解決方案。關於這個問題有很多答案。然而,任何人都解決了我的問題(也是另一個人的問題)。我發現了一些關於這樣的問題的錯誤報告。但是每個報告都應該在.Net Connector 6.8.3中解決。 我覺得我缺少的EF非常簡單的步驟,與MySQL,但我不能找到什麼是我的錯誤...
示例代碼:
using (prodEntities myEntity = new prodEntities())
{
userloginlog log = new userloginlog();
log.UserRef = pUserID;
log.Success = pSuccess;
log.IpNo = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
log.BrowserInfo = browserInfo;
log.LoginTime = GeneralHelperC.getCompanyDateTime();
myEntity.userloginlog.Add(log);
myEntity.SaveChanges();
}
表:
CREATE TABLE `userloginlog` (
`RID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`UserRef` bigint(20) unsigned NOT NULL,
`LoginTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`IpNo` varchar(15) DEFAULT NULL,
`Success` tinyint(4) NOT NULL,
`BrowserInfo` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`RID`),
KEY `userLoginLog_Staff_Key` (`UserRef`),
CONSTRAINT `UserLoginLog_User_Key` FOREIGN KEY (`UserRef`) REFERENCES `users` (`RID`)
) ENGINE=InnoDB AUTO_INCREMENT=16903 DEFAULT CHARSET=utf8;
例外:
at MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SqlFragment.ToString()
at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(Dictionary`2 identifierValues)
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__d()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClassb.<SaveChangesInternal>b__8()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
安裝在我的電腦裏:
- MySQL服務器5.6CE
- MySQL連接NET 6.8.3
- 的Visual Studio 2013
Refenced DLL的項目(與安裝的NuGet每個人):
- MySql.Data .dll(6.8.3)
- MySql.Data.Entity.dll(6.7.5)
- MySql.Data.Entities.dll(6.8.3.0)
- MySql.Web.dll(6.8.3)
- EntityFramework.dll(6.1.1)
項目是Asp.Net網絡表單,其下運行在.NET Framework 4.0
非常感謝您
你可以發佈實體類嗎? –
你已經發布了9個月,你有沒有發現問題的根源和解決方法?我自己得到完全相同的錯誤 – Ghostrider