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