Last active
August 29, 2015 14:15
-
-
Save kiichi/5df2d3937bb67d4c67d4 to your computer and use it in GitHub Desktop.
.NET Entity Framework Application Context and Initializer with Seed function example
This file contains hidden or 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.Web; | |
| using System.Data; | |
| using System.Data.Entity; | |
| using System.Data.Entity.Validation; | |
| using System.Data.Entity.InfrastMyAppructure; | |
| using System.Configuration; | |
| namespace MyCompany.HR { | |
| /// <summary> | |
| /// Summary description for ApplicationContext | |
| /// </summary> | |
| public class MyAppContext : DbContext { | |
| public DbSet<Job> Jobs { get; set; } | |
| // etc... | |
| public MyAppContext() | |
| : base("name=MyAppContext") { | |
| // avoid lazyloading | |
| //this.Configuration.LazyLoadingEnabled = false; | |
| //this.Configuration.ProxyCreationEnabled = false; | |
| } | |
| protected override void OnModelCreating(DbModelBuilder modelBuilder) { | |
| base.OnModelCreating(modelBuilder); | |
| modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>(); | |
| // Add any configuration or mapping stuff here | |
| } | |
| /// <summary> | |
| /// Feed test data | |
| /// </summary> | |
| /// <param name="Context"></param> | |
| public void Seed(MyAppContext Context) { | |
| // Need something from App_Data? | |
| // HostingEnvironment.MapPath("~/App_Data/something.csv"); | |
| // add some data | |
| Context.SaveChanges(); | |
| } | |
| public class DropCreateAlwaysInitializer : DropCreateDatabaseAlways<MyAppContext> { | |
| protected override void Seed(MyAppContext context) { | |
| context.Seed(context); | |
| base.Seed(context); | |
| } | |
| } | |
| public class DropCreateIfChangeInitializer : DropCreateDatabaseIfModelChanges<MyAppContext> { | |
| protected override void Seed(MyAppContext context) { | |
| context.Seed(context); | |
| base.Seed(context); | |
| } | |
| } | |
| public class CreateInitializer : CreateDatabaseIfNotExists<MyAppContext> { | |
| protected override void Seed(MyAppContext context) { | |
| context.Seed(context); | |
| base.Seed(context); | |
| } | |
| } | |
| static MyAppContext() { | |
| ///////////////////////////////////////////////// | |
| //use master | |
| //ALTER DATABASE MyCompanyHR SET SINGLE_USER WITH ROLLBACK IMMEDIATE | |
| //ALTER DATABASE MyCompanyHR SET MULTI_USER | |
| ///////////////////////////////////////////////// | |
| //#if DEBUG | |
| // Database.SetInitializer<ApplicationContext> (new DropCreateIfChangeInitializer ()); | |
| //#else | |
| // Database.SetInitializer<ApplicationContext>(new CreateInitializer()); | |
| //#endif | |
| // Comment out below as you need | |
| //Database.SetInitializer<MyAppContext>(new CreateInitializer()); | |
| //Database.SetInitializer<MyAppContext>(new DropCreateIfChangeInitializer()); | |
| //Database.SetInitializer<MyAppContext>(null); | |
| if (ConfigurationManager.AppSettings["FORCE_INIT"] == "YES") | |
| { | |
| Database.SetInitializer<MyAppContext>(new DropCreateAlwaysInitializer()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment