Created
March 31, 2014 06:19
-
-
Save davidroth/9886349 to your computer and use it in GitHub Desktop.
EntityFramwork DbConfiguration with custom DbModelStore for loading cached db models. The custom DbModelStore invalidates out-of-date cache files by comparing the last update date of the cache-xml to the update date of the domain assembly.
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
public class MyContextConfiguration : DbConfiguration | |
{ | |
public MyContextConfiguration() | |
{ | |
MyDbModelStore cachedDbModelStore = new MyDbModelStore(MyContext.EfCacheDirPath); | |
IDbDependencyResolver dependencyResolver = new SingletonDependencyResolver<DbModelStore>(cachedDbModelStore); | |
AddDependencyResolver(dependencyResolver); | |
} | |
private class MyDbModelStore : DefaultDbModelStore | |
{ | |
public MyDbModelStore(string location) | |
: base(location) | |
{} | |
public override DbCompiledModel TryLoad(Type contextType) | |
{ | |
string path = GetFilePath(contextType); | |
if(File.Exists(path)) | |
{ | |
DateTime lastWriteTime = File.GetLastWriteTimeUtc(path); | |
DateTime lastWriteTimeDomainAssembly = File.GetLastWriteTimeUtc(typeof(MyDomain.SomeTypeInOurDomain).Assembly.Location); | |
if (lastWriteTimeDomainAssembly > lastWriteTime) | |
{ | |
File.Delete(path); | |
Tracers.EntityFramework.TraceInformation("Cached db model obsolete. Re-creating cached db model edmx."); | |
} | |
} | |
else | |
{ | |
Tracers.EntityFramework.TraceInformation("No cached db model found. Creating cached db model edmx."); | |
} | |
return base.TryLoad(contextType); | |
} | |
} | |
} |
Hi, each time i run the application it rewrites .edmx files, that's why there is no luck, can you please advise what could be wrong? i debug the code and sure this line File.Delete(path); is not deleting the existing file. thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can u explain for me what is MyContext.EfCacheDirPath (i'm a beginer)