Last active
August 29, 2015 14:08
-
-
Save javiercampos/9b95e910879e51cc65ed to your computer and use it in GitHub Desktop.
Entity Framework Migrations Seed Debug Window
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; | |
using System.Collections.Generic; | |
using System.Data.Entity.Migrations; | |
using System.Linq; | |
using System.Reflection; | |
using System.Windows.Forms; | |
using Data.Persistence.Migrations.SeedDebug; | |
namespace Data.Persistence.Migrations | |
{ | |
internal sealed class Configuration : DbMigrationsConfiguration<MyContext> | |
{ | |
public Configuration() | |
{ | |
// Migrations configuration here | |
} | |
protected override void Seed(MyContext context) | |
{ | |
// Create our application context which will host our debug window and message loop | |
var appContext = new SeedApplicationContext<MyContext>(SeedInternal, context, false); | |
Application.Run(appContext); | |
var e = appContext.Exception; | |
Application.Exit(); | |
// Rethrow the exception to the package manager console | |
if (e != null) | |
throw e; | |
} | |
// Our original Seed method, now with Trace support! | |
private void SeedInternal(MyContext context) | |
{ | |
// ... | |
Trace.WriteLine("I'm seeding!") | |
// ... | |
} | |
} | |
} |
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.Data.Entity; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace Data.Persistence.Migrations.SeedDebug | |
{ | |
public class SeedApplicationContext<T> : ApplicationContext | |
where T : DbContext | |
{ | |
private class SeedTraceListener : TraceListener | |
{ | |
private readonly SeedApplicationContext<T> _appContext; | |
public SeedTraceListener(SeedApplicationContext<T> appContext) | |
{ | |
_appContext = appContext; | |
} | |
public override void Write(string message) | |
{ | |
_appContext.WriteDebugText(message); | |
} | |
public override void WriteLine(string message) | |
{ | |
_appContext.WriteDebugLine(message); | |
} | |
} | |
private Form _debugForm; | |
private TextBox _debugTextBox; | |
private TraceListener _traceListener; | |
private readonly Action<T> _seedAction; | |
private readonly T _dbcontext; | |
public Exception Exception { get; private set; } | |
public bool WaitBeforeExit { get; private set; } | |
public SeedApplicationContext(Action<T> seedAction, T dbcontext, bool waitBeforeExit = false) | |
{ | |
_dbcontext = dbcontext; | |
_seedAction = seedAction; | |
WaitBeforeExit = waitBeforeExit; | |
_traceListener = new SeedTraceListener(this); | |
CreateDebugForm(); | |
MainForm = _debugForm; | |
Trace.Listeners.Add(_traceListener); | |
} | |
private void CreateDebugForm() | |
{ | |
var textbox = new TextBox {Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Both, WordWrap = false}; | |
var form = new Form {Font = new Font(@"Lucida Console", 8), Text = "Seed Trace"}; | |
form.Controls.Add(tb); | |
form.Shown += OnFormShown; | |
_debugForm = form; | |
_debugTextBox = textbox; | |
} | |
private void OnFormShown(object sender, EventArgs eventArgs) | |
{ | |
WriteDebugLine("Initializing seed..."); | |
try | |
{ | |
_seedAction(_dbcontext); | |
if(!WaitBeforeExit) | |
_debugForm.Close(); | |
else | |
WriteDebugLine("Finished seed. Close this window to continue"); | |
} | |
catch (Exception e) | |
{ | |
Exception = e; | |
var einner = e; | |
while (einner != null) | |
{ | |
WriteDebugLine(string.Format("[Exception {0}] {1}", einner.GetType(), einner.Message)); | |
WriteDebugLine(einner.StackTrace); | |
einner = einner.InnerException; | |
if (einner != null) | |
WriteDebugLine("------- Inner Exception -------"); | |
} | |
} | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing && _traceListener != null) | |
{ | |
Trace.Listeners.Remove(_traceListener); | |
_traceListener.Dispose(); | |
_traceListener = null; | |
} | |
base.Dispose(disposing); | |
} | |
private void WriteDebugText(string message) | |
{ | |
_debugTextBox.Text += message; | |
Application.DoEvents(); | |
} | |
private void WriteDebugLine(string message) | |
{ | |
WriteDebugText(message + Environment.NewLine); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment