Entity Framework 6 with Mysql and NullReferenceException

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

Question

When I try to insert same data I am getting NullReferenceException exception.

I have read tons of posts in Stackoverflow, asp.net forums and mysql community forums. I could not found any true solution for this problem. There is a lots of answer about that. However anyone solved my problem (also another people's problems). I found some bug reports about problem like that. However every reports should solved in .Net Connector 6.8.3. I think I am missing very simple step in EF with Mysql but I could not found what is my mistakes...

Sample Code:

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();
}

Table:

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()

Installed in my computer:

  • Mysql Server 5.6CE
  • MySQL Connector NET 6.8.3
  • Visual Studio 2013

Refenced DLL's in project (everyone installed with 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)

Project is Asp.Net Webform which running under .Net Framework 4.0

Thank you very much

1
2
7/3/2014 4:54:31 AM

Popular Answer

We had a similar issue. We didn't have an ID field, but we had a timestamp as primary key. The solution was to introduce an ID, and set that to be the primary key.

1
10/11/2015 11:00:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow