// I spend ages on this...but it won't work. // Even though Db Modeling can work via reflection // even when invoked from powershell (add-migration) // This approach will work from code, but not powershell (update-database) // You *have* to define Seed elements by hand. // In one way it's better. There's an order which must be followed // when seeding, that can be done via Attributes ([After/Before]) // but for now this is ok. private void SeedByReflection(AppModuleDbContext context) { AppDependencyLocator.Current.GetAllInstances <IHasAppModuleDbContextSeedInitializer>() .ForEach(x => { if (!(typeof(IHasIgnoreThis).IsAssignableFrom(x.GetType()))) { x.Seed(context); } }); }
public void Seed(AppModuleDbContext context) { CodeFirstMigrationConfigurationSettings debuggerConfiguration = this._hostSettingsService.GetObject <CodeFirstMigrationConfigurationSettings>(); SeedImmutableEntries(context); if (debuggerConfiguration.CodeFirstSeedDemoStuff) { SeedDevOnlyEntries(context); } }
// This method will be called after migrating to the latest version. public void Initialize(AppModuleDbContext context) { // if (!PowershellServiceLocatorConfig.Activated) // { // // Actually, Seeding needs to be done in a specific order // // so for now ByHand is preferable // SeedByReflection(context); // } // else // { // //Reflection does not work under Powershell, so: SeedByHand(context); // } }
protected void SeedDevOnlyEntries(AppModuleDbContext context) { var records = new[] { new Example { Id = 1.ToGuid(), Owner = "jSmith", PublicText = "Some publicly viewable Text...", SensitiveText = "Some sensitive Data (eg: GovId:ABC1234)", AppPrivateText = "Some App-Private Text (you should *not* see it)..." } }; var dbSet = context.Set <Example>(); dbSet.AddOrUpdate(p => p.Id, records); context.SaveChanges(); }
private void SeedByHand(AppModuleDbContext dbContext) { AttachDebuggerWhenRunningUnderPowershell(); AppDependencyLocator.Current.GetInstance <AppModuleDbContextSeederExample>().Seed(dbContext); }
/// <summary> /// Seed records that are part of this Module, no matter what (Immutable). /// <para> /// NOT the right place for demo entries, or data that will be updated /// by end users. /// </para> /// </summary> /// <param name="context"></param> protected void SeedImmutableEntries(AppModuleDbContext context) { }