Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有Mysql和NullReferenceException的实体框架6_C#_Mysql_Asp.net_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 具有Mysql和NullReferenceException的实体框架6

C# 具有Mysql和NullReferenceException的实体框架6,c#,mysql,asp.net,entity-framework,entity-framework-6,C#,Mysql,Asp.net,Entity Framework,Entity Framework 6,当我尝试插入相同的数据时,得到的是NullReferenceException 我在Stackoverflow、asp.net论坛和mysql社区论坛上读了很多帖子。 我找不到解决这个问题的真正办法。关于这个问题有很多答案。然而,任何人都解决了我的问题(也解决了其他人的问题)。我发现了一些关于这类问题的错误报告。但是,每个报告都应在.Net Connector 6.8.3中解决。 我想我错过了使用Mysql的EF中非常简单的一步,但我找不到我的错误 示例代码: using (prodEntiti

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

我在Stackoverflow、asp.net论坛和mysql社区论坛上读了很多帖子。 我找不到解决这个问题的真正办法。关于这个问题有很多答案。然而,任何人都解决了我的问题(也解决了其他人的问题)。我发现了一些关于这类问题的错误报告。但是,每个报告都应在.Net Connector 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.Data.Entity.ListFragment.WriteSql(StringBuilderSQL)
位于MySql.Data.Entity.SelectStatement.WriteSql(StringBuilderSQL)
位于MySql.Data.Entity.InsertStatement.WriteSql(StringBuilderSQL)
位于MySql.Data.Entity.SqlFragment.ToString()处
在MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree树)
位于MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree)
位于System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest、DbCommandTree commandTree、DbInterceptionContext interceptionContext)
位于System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Core.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree,DbInterceptionContext interceptionContext)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
位于System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(Dictionary`2 IdentifierValue)
在System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues,List`1 generatedValues)
位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()处
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult,Func`2 updateFunction)
位于System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()处
在System.Data.Entity.Core.Objects.ObjectContext.b_ud()中
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDBEcutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项、IDBEcutionStrategy executionStrategy、Boolean startLocalTransaction)
在System.Data.Entity.Core.Objects.ObjectContext.c__DisplayClassb.b__8()中
在System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1操作)中
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions选项,布尔值executeInExistingTransaction)
位于System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions)
位于System.Data.Entity.Internal.InternalContext.SaveChanges()处
在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()中
位于System.Data.Entity.DbContext.SaveChanges()处
安装在我的计算机中:

  • Mysql服务器5.6CE
  • MySQL连接器网络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)
该项目是在.NETFramework 4.0下运行的Asp.NETWebForm


非常感谢

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

您不需要您的
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>

当我仔细查看时,EF将表中的所有字段都视为键。不幸的是,我没有进行调查,而是从头开始重新创建表,从模型中删除实体类型,然后从数据库中重新导入它们


之后问题就消失了。

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

我和Balaz Somogyi遇到了相同的问题,这与OP的问题类似(我引用的是MySQL.Data和MySQL.Data.Entity.EF6的6.9.7版)。我有一个日志表,主键是
时间戳
字段。当实体框架生成
.edmx
时,它将主键字段上的
StoreGeneratedPattern
属性设置为
Identity
。由于我不依赖实体框架返回插入的
时间戳
,因此我选择将
StoreGeneratedPattern
属性设置为
None
,而不是更改数据库中的表结构。这为我解决了这个问题。

您能发布实体类吗?您发布实体类已经9个月了,您是否找到了根本原因和解决方法?我得到了完全相同的错误我自己Também estou com o mesmo erro!实际上我有两个项目。另一个是实体模型项目。实体模型项目引用自Web项目。在您回答之后,我尝试在Web Project中创建新模型,但我遇到了错误:“您的项目引用了Entity Framework的最新版本;但是,在y中找不到与此版本兼容的Entity Framework数据库提供程序