2014-07-03 79 views
2

當我嘗試插入相同的數據時,我得到NullReferenceException異常。實體框架6與Mysql和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; 

例外:

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

Refenced 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網絡表單,其下運行在.NET Framework 4.0

非常感謝您

+0

你可以發佈實體類嗎? –

+0

你已經發布了9個月,你有沒有發現問題的根源和解決方法?我自己得到完全相同的錯誤 – Ghostrider

回答

1

如果您正在使用實體框架6,正確的MySQL數據實體使用的是MySql.Data.Entity.EF6。我有一個Webforms項目,我使用VS 2013和MySql。 MySQL的引用是這樣的:

enter image description here

你不需要你​​或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> 
+0

其實我有兩個項目。其中一個網絡是實體模型項目。實體模型項目是從Web項目引用的。你的答案我試圖在Web Project中創建新的模型,但我得到了這個錯誤: 「你的項目引用了最新版本的實體框架;但是,與你的數據連接找不到與這個版本兼容的實體框架數據庫提供者。退出此嚮導,安裝兼容的提供程序,然後在執行此操作之前重新生成項目「 發生此錯誤後,我有一些搜索,但再次解決了我的問題。 – Dreamcatcher

0

正如我拍了仔細一看,EF正考慮在有關爲密鑰表中的所有領域。不幸的是,我沒有按時完成任務,而是從頭開始重新創建表,從模型中刪除實體類型,並從數據庫中重新導入它們。

之後,問題就消失了。

1

我們有類似的問題。我們沒有ID字段,但我們有一個時間戳作爲主鍵。解決方案是引入一個ID,並將其設置爲主鍵。

0

我一直和Balaz Somogyi有類似的問題(我引用了MySQL.Data和MySql.Data.Entity.EF6的6.9.7版本)。我有一個以TimeStamp字段爲主鍵的日誌表。當實體框架生成.edmx時,它將主鍵字段上的StoreGeneratedPattern屬性設置爲Identity。由於我不依賴實體框架返回插入的TimeStamp我選擇將StoreGeneratedPattern屬性設置爲None,而不是更改數據庫中的表結構。這爲我解決了這個問題。