【发布时间】:2014-08-24 01:50:09
【问题描述】:
当我尝试插入相同的数据时,我收到 NullReferenceException 异常。
我在 Stackoverflow、asp.net 论坛和 mysql 社区论坛中阅读了大量帖子。 对于这个问题,我找不到任何真正的解决方案。对此有很多答案。然而,任何人都解决了我的问题(也是其他人的问题)。我发现了一些关于此类问题的错误报告。但是,每个报告都应在 .Net 连接器 6.8.3 中解决。 我想我在使用 Mysql 的 EF 中缺少非常简单的步骤,但我找不到我的错误...
示例代码:
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
项目中引用的 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)
项目是在.Net Framework 4.0下运行的Asp.Net Webform
非常感谢
【问题讨论】:
-
可以发实体类吗?
-
你发帖已经9个月了,你有没有找到根本原因和解决方法?我自己也遇到了完全相同的错误
-
Também estou com o mesmo erro!
标签: c# mysql asp.net entity-framework entity-framework-6