0
我最近採取了由另一個同事完成的CF5項目,我想添加一個新功能,但我有一些錯誤。所以,我想爲這個應用程序創建一個自動創建和自動遷移系統。EF5代碼首先遷移 - 創建數據庫和遷移錯誤
我已經添加了這個檢查在app.cs構造數據庫狀態:
using (var db = new DatabaseContext())
{
do
{
Cursor.Current = Cursors.WaitCursor;
// If DB already exists, tests his integrity
if (db.Database.Exists() == true)
{
bool isCompatible = false;
bool isMetadataMissing = false;
// Tests compatibility between DB and EF model
try
{
isCompatible = db.Database.CompatibleWithModel(true);
}
catch (Exception e)
{
// Exception has thrown because the database don't contains metadata informations
isCompatible = false;
isMetadataMissing = true;
m_Logger.Error("Error during checking compatibility of database : " + e.Message, e);
}
// If database is compatible, we quit de tests
if (isCompatible)
{
result = true;
isWorkRequired = false;
m_Logger.Debug("Database Ok");
Cursor.Current = Cursors.Default;
}
else
{
// If DB isn't compatible and metadata was found on database, we proceed the migration of database
if (!isMetadataMissing)
{
try
{
m_Logger.Debug("Migration needed on database");
db.Database.Initialize(true);
}
catch (Exception eMig)
{
result = false;
isWorkRequired = false;
m_Logger.Debug("Can't migrates database. "+eMig.Message);
Cursor.Current = Cursors.Default;
messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.ErrorDBMetaError,
MessageBoxButton.OK, MessageBoxImage.Error);
messageBoxView = new MessageBoxView(messageBoxViewModel);
messageBoxView.ShowDialog();
}
}
else // Warns the user that there is a problem with the ConnexionString
{
result = false;
isWorkRequired = false;
m_Logger.Debug("Can't update database, the metadata is missing");
Cursor.Current = Cursors.Default;
messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.ErrorDBMetaMissing,
MessageBoxButton.OK,MessageBoxImage.Error);
messageBoxView = new MessageBoxView(messageBoxViewModel);
messageBoxView.ShowDialog();
}
}
}
else
{
m_Logger.Debug("Database is Mising");
Cursor.Current = Cursors.Default;
messageBoxViewModel = new MessageBoxViewModel(Resources.Resources.QtCreateDB,
MessageBoxButton.YesNo,MessageBoxImage.Question);
messageBoxView = new MessageBoxView(messageBoxViewModel);
messageBoxView.ShowDialog();
if (messageBoxViewModel.Result == MessageBoxResult.Yes)
{
Cursor.Current = Cursors.WaitCursor;
m_Logger.Debug("Creating database");
db.Database.Create();
//db.Database.Initialize(true);
m_Logger.Debug("Adds roles objects");
// Adds initial data
m_Logger.Debug("Database created");
}
else
{
isWorkRequired = false;
}
}
} while (isWorkRequired);
}
之後,該數據庫已創建,但沒有應用以前遷移:-s 所以,如果我想要做「添加遷移」控制檯告訴我有一些遷移正在等待更改。
因此,我做了「update-database」manualy,但我得到這個錯誤:「每個表中的列名必須是唯一的。」dbo.Segment「表中的列名'TrackName'被指定多次」
爲什麼我在創建數據庫時沒有應用遷移?
任何人都可以幫助我嗎?我是新與CF5 :-) 謝謝
我猜你正在使用代碼優先。你可以添加Segment實體的模型配置嗎? – margabit 2013-04-26 08:38:31
看起來像映射配置的問題。您應該發佈模型的圖像(如果您先應用模型)或映射配置文件(如果您先應用代碼)。 – 2013-04-26 08:39:07