Created
July 14, 2015 21:25
-
-
Save matthewrenze/8101479dcc28d343c874 to your computer and use it in GitHub Desktop.
Automatically Create Database in Entity Framework 6 with Automatic Migrations Disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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