首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有Mysql和NullReferenceException的实体框架6

带有Mysql和NullReferenceException的实体框架6
EN

Stack Overflow用户
提问于 2014-07-03 04:54:31
回答 4查看 2.2K关注 0票数 2

当我尝试插入相同的数据时,我得到的是NullReferenceException异常。

我在Stackoverflow、asp.net论坛和mysql社区论坛上读过大量的文章。对于这个问题,我找不到真正的解决办法。关于这个问题有很多答案。然而,有人解决了我的问题(也是别人的问题)。我发现了一些关于这样的问题的错误报告。但是,每个报告都应该在.Net连接器6.8.3中解决。我认为我错过了非常简单的步骤,在EF与Mysql,但我找不到我的错误是什么。

样本代码:

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

表:

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

例外:

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

安装在我的计算机中:

  • MySQLServer5.6CE
  • MySQL连接器网6.8.3
  • 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)

项目是运行在Asp.Net框架4.0下的.Net Webform

非常感谢

EN

回答 4

Stack Overflow用户

发布于 2014-07-03 10:44:47

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

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

对于您的Web.config:

代码语言:javascript
运行
复制
 <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>
票数 1
EN

Stack Overflow用户

发布于 2015-10-11 23:00:34

我们也有过类似的问题。我们没有ID字段,但是我们有一个时间戳作为主键。解决方案是引入一个ID,并将其设置为主键。

票数 1
EN

Stack Overflow用户

发布于 2015-04-25 04:06:41

当我仔细观察时,EF正在考虑表中所有的字段作为键。不幸的是,我没有及时完成任务,而是从头开始重新创建了表,从模型中删除了实体类型,然后从数据库中重新导入它们。

从那以后,问题就消失了。

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

https://stackoverflow.com/questions/24545216

复制
相关文章

相似问题

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