Skip to content

Instantly share code, notes, and snippets.

@matthewrenze
Created July 14, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewrenze/8101479dcc28d343c874 to your computer and use it in GitHub Desktop.
Save matthewrenze/8101479dcc28d343c874 to your computer and use it in GitHub Desktop.
Automatically Create Database in Entity Framework 6 with Automatic Migrations Disabled
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6CreateDatabaseWithoutAutomaticMigrations
{
public class DatabaseConfiguration : DbMigrationsConfiguration<MyContext>
{
public DatabaseConfiguration()
{
this.AutomaticMigrationsEnabled = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6CreateDatabaseWithoutAutomaticMigrations
{
public class Entity
{
public int EntityId { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6CreateDatabaseWithoutAutomaticMigrations
{
public class MyContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
public MyContext()
{
Database.SetInitializer<MyContext>(null);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF6CreateDatabaseWithoutAutomaticMigrations
{
class Program
{
static void Main(string[] args)
{
var myContext = new MyContext();
myContext.Database.CreateIfNotExists();
var newEntity = new Entity()
{
Name = "Test"
};
myContext.Entities.Add(newEntity);
myContext.SaveChanges();
foreach (var oldEntity in myContext.Entities)
Console.WriteLine(oldEntity.EntityId + " - " + oldEntity.Name);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment