I'm unsure about how to use the code first migration feature. In my understanding it should create my database if it's not existing already, and update it to the latest schema according to migration files. But I'm struggling with it, because I always get a lot of errors and I'm unsure overall how to use this properly..
internal class Program
{
private static void Main()
{
EntityFrameworkProfiler.Initialize();
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>());
using (var context = new MyContext())
{
var exists = context.Database.Exists();
if (!exists)
{
context.Database.Create();
}
var element = context.Dummies.FirstOrDefault();
}
}
}
public class MyContext : DbContext
{
public MyContext()
: base(string.Format(@"DataSource=""{0}""", @"C:\Users\user\Desktop\MyContext.sdf"))
{
}
public DbSet<Dummy> Dummies { get; set; }
}
internal sealed class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(CodeFirstTest.MyContext context)
{
}
}
Using the Entity Framework Profiler I check what statements are executed. When I run the program with no database existing I get the following output:
-- statement #1 SELECT [GroupBy1].[A1] AS [C1] FROM (SELECT COUNT(1) AS [A1] FROM [__MigrationHistory] AS [Extent1]) AS [GroupBy1]
-- statement #2 WARN: System.Data.SqlServerCe.SqlCeException (0x80004005): The specified table does not exist. [ __MigrationHistory ] at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) at System.Data.