2015-04-06 22 views
1

我無法使用AddObject方法將數據插入數據庫使用實體框架版本5.0。請幫助我通過。無法使用AddObject方法將數據插入數據庫使用實體框架版本5.0

List<Error> errorlist = new List<Error>(); 
    errorlist.Add(new Error{ Authentication="From", dateTime=DateTime.Now.ToString(), messageText="xyz", server="10.11.12.217", Username="John"}); 
    errorlist.Add(new Error{ Authentication="From", dateTime=DateTime.Now.ToString(), messageText="xyz", server="10.11.12.217", Username="George"}); 
    using (TransactionScope transe = new TransactionScope()) 
    { 
     using (SportsEntities bulk = new SportsEntities()) 
     { 
      ErrorLog error = new ErrorLog(); 
      for (int i = 0; i < errorlist.Count; i++) 
      { 
       error.AUTHENTICATION_MODE = errorlist[i].Authentication; 
       error.DATE_ARRIVAL = errorlist[i].dateTime; 
       error.MESSAGE = errorlist[i].messageText; 
       error.SERVER_DETAILS = errorlist[i].server; 
       error.USERNAME = errorlist[i].Username; 
       bulk.ErrorLogs.AddObject(error); 
      } 
      bulk.SaveChanges(); 
     } 
     transe.Complete(); 
    } 
} 
+0

嘗試'bulk.ErrorLogs.Add(error);'(只是'.Add()' - 不是'.AddObject()')..... – 2015-04-06 08:19:21

回答

0

您可以使用.Add()而不是此.AddObject()。 但是如果你仍然想使用它:

雙擊你的.edmx文件,按F4或右鍵單擊並轉到屬性。在這裏你會看到「代碼生成策略」:T4。將其更改爲「遺留對象上下文」。這將在designer.cs中創建必要的文件。現在刪除你的tt文件和由T4生成策略創建的Context.tt。你將能夠使用.AddObject()。

.AddObject()是Object Context的屬性。 .Add()是DbContext的。

相關問題