EF 热加载 Winform/Asp.net
public partial class Form1 : Form { BackgroundWorker worker = new BackgroundWorker(); xxContext context_ = null; public Form1() { InitializeComponent(); worker.DoWork += Worker_DoWork; worker.RunWorkerCompleted += Worker_RunWorkerCompleted; worker.RunWorkerAsync(); } private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void Worker_DoWork(object sender, DoWorkEventArgs e) { context_ = new xxContext(); var objectContext = ((IObjectContextAdapter)dbcontext_).ObjectContext; var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappingCollection.GenerateViews(new List<EdmSchemaError>()); } }
public class InitModule : IHttpModule { public static bool flag = false; #region IHttpModule Members public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += Context_BeginRequest; } private void Context_BeginRequest(object sender, EventArgs e) { if (!flag) { flag = true; Task.Factory.StartNew(() => { var context = new xxContext(); var objectContext = ((IObjectContextAdapter)context).ObjectContext; var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace); mappingCollection.GenerateViews(new List<EdmSchemaError>()); }); } } #endregion }
web.config:
<system.webServer> <modules> <add name="init" type="InitModule"/> </modules> </system.webServer>