我收到以下錯誤:實體框架的錯誤:上下文改變,因爲數據庫創建
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();
}
}
}
}
非常感謝
http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –
我試過了:)不多說 – user2330270
更新你的數據庫上下文也許吧?你知道該怎麼做? –