c# - 带有 Mysql 和 NullReferenceException 的 Entity Framework 6

标签 c# mysql asp.net entity-framework entity-framework-6

当我尝试插入相同的数据时,我收到 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;

异常(exception):

   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

非常感谢

最佳答案

如果您使用的是 Entity Framework 6,则要使用的正确 MySql 数据实体是 MySql.Data.Entity.EF6。我有一个 Webforms 项目,我正在使用 VS 2013 和 MySql。 Mysql 引用如下所示:

enter image description here

您不需要 MySql.Data.Entity.dll (6.7.5)MySql.Data.Entities.dll (6.8.3.0)

对于您的 Web.config:

 <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient"
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider"
           invariant="MySql.Data.MySqlClient"
           description=".Net Framework Data Provider for MySQL"
           type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,  Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

关于c# - 带有 Mysql 和 NullReferenceException 的 Entity Framework 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24545216/

相关文章:

c# - AjaxToolkit ModalPopupExtender 不在顶部

c# - 使用 .net http2 的 Apple 推送通知

c# - 如何从 DirectoryEntry 和 DN 检索 DirectoryEntry

php - 当 Bind_param() 与 UPDATE 语句一起使用时,列计数与第 1 行的值计数不匹配

mysql - 将数据库字段保存为 YYYY-MM-DD;需要显示并排序为 DD-MM-YYYY

MySQL - 在没有 LIMIT 或 JOIN 的情况下选择指定搜索的最大值

c# - 服务器端 AJAX 请求的排队机制

c# - .net 在文本框中大写名称

c# - 展平 IEnumerable<IEnumerable<>>;理解泛型

c# - 如何在控制台应用程序 C# 中获取 server.Mappath