2014-02-10 47 views
0

我收到以下錯誤:實體框架的錯誤:上下文改變,因爲數據庫創建

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The model backing the 'PrivateMessageContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/ ?

但它爲何改變?我想要的是得到新的消息,檢查用戶有以前的消息歷史記錄,並創建一個新的消息歷史或追加其目前的...

我想測試發送方法,所以我要打印走出一條新信息添加

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data.Entity; 

namespace CodeFirstNewDatabaseSample 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
      Console.Write("Enter message: "); 
      var message = Console.ReadLine(); 
      var userFrom = "userFrom"; 
      var userTo = "userTo"; 
      Methods test = new Methods(); 
      for(var i=0; i < 10; i++) 
      { 
       test.StoreMessage(userTo, userFrom, message); 
       Console.Write("Enter message: "); 
       message = Console.ReadLine(); 
      } 
    } 
} 
public class PrivateMessageContext : DbContext 
{ 
    public DbSet<PrivateMessageHeader> PrivateMessages { get; set; } 
} 
public class PrivateMessageHeader 
{ 
    public PrivateMessageHeader() { this.Messages = new List<PrivateMessageDetail>(); } 
    public int PrivateMessageHeaderId { get; set; } 
    public DateTime TimeStamp { get; set; } // Date of the start of thread 
    public string User1 { get; set; } 
    public string User2 { get; set; } // this could be made to a list to allow multiples 

    public ICollection<PrivateMessageDetail> Messages { get; set; } 
} 

public class PrivateMessageDetail 
{ 
    public int PrivateMessageDetailId { get; set; } 
    public DateTime MessageDate { get; set; } 
    public string FromUser { get; set; } // Don't need ToUser, it's already in header 
    public string Message { get; set; } 

    public PrivateMessageHeader parent { get; set; } 
} 
public class Methods 
{ 

    public void StoreMessage(string userTo, string userFrom, string InputMessage) 
    { 
     using (var db = new PrivateMessageContext()) 
     { 
      var collection = from pm in db.PrivateMessages select pm; 
      foreach(var msg in collection) 
      { 
       var user1 = msg.User1; 
       var user2 = msg.User2; 
       if ((user1==userTo && user2==userFrom) || (user1==userFrom && user2==userTo)) 
       { 
        var msgDetail = new PrivateMessageDetail(); 
        msgDetail.FromUser = userFrom; 
        msgDetail.Message = InputMessage; 
        msgDetail.MessageDate = DateTime.Now; 
        msg.Messages.Add(msgDetail); 
        db.SaveChanges(); 
        return; 
       } 
      } 
      // pair doesn't exist 

      var PrivateMessage = new PrivateMessageDetail(); 
      PrivateMessage.MessageDate = DateTime.Now; 
      PrivateMessage.FromUser = userFrom; 
      PrivateMessage.Message = InputMessage; 
      var newCollection = new PrivateMessageHeader(); 
      newCollection.TimeStamp = DateTime.Now; 
      newCollection.User1 = userTo; 
      newCollection.User2 = userFrom; 
      newCollection.Messages.Add(PrivateMessage); 
      db.PrivateMessages.Add(newCollection); 
      db.SaveChanges(); 

      var iterator = 0; 
      // Display all messages from the database 
      foreach (var pmsg in db.PrivateMessages) 
      { 

       var query = pmsg; 
       var list = pmsg.Messages.ToList(); 
       foreach (var item in list) 
       { 
        Console.WriteLine("msg" + iterator + ": " + item.ToString()); 
       } 
       iterator++; 
      } 
      Console.ReadKey(); 
     }  
    } 
} 

}

非常感謝

+0

http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –

+0

我試過了:)不多說 – user2330270

+0

更新你的數據庫上下文也許吧?你知道該怎麼做? –

回答

0

一個我用這個問題的來源是包管理器控制檯在VS2012。通過使用Enable-Migrations命令以及EnableAutomaticMigations標誌,然後將遷移信息添加到項目中,我可以使用不會「破壞」數據庫或我的代碼的任何Code-First模型更改來更新數據庫。你不必手動添加任何東西到項目中,這一切都是爲你完成的。如果您忘記啓用自動遷移,則可以在此過程中添加的配置文件中手動編輯它。我相信這個過程自VS2008以來一直存在,但不要引用我的話。自從VS2010以來,我一直使用它,並且在Web和桌面上都使用MVC和MVVM。我在MSDN上使用的文章之一是:http://msdn.microsoft.com/en-us/data/JJ591621.aspx,你也可以從John Galloway的教程中找到詳細解釋。

0

您是否嘗試過Database.SetInitializer<PrivateMessageContext>(null);每次先前的消息?

嘗試更改您的上下文,如下所示。

public class PrivateMessageContext : DbContext 
{ 
    public DbSet<PrivateMessageHeader> PrivateMessages { get; set; } 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
     Database.SetInitializer<PrivateMessageContext>(null); 
    } 

} 

更多信息here

+0

謝謝你,幫助,但現在我有一個未處理的異常類型'System.Data.Entity.Core.EntityCommandExecutionException'發生在mscorlib.dll中的行foreach(集合中的var msg)在StoreMessage中。 – user2330270

+0

檢查您的連接字符串,並確保數據庫中存在特定的表。 – Zafar

相關問題