locked
Entity Framework 6.1.3 Code first generating EDMX at runtime RRS feed

  • Question

  • We are using Code First EntityFramework 6.1.3 (MSSQL 2014) and were having issues with performance of LINQ queries.

    The first query would take 3.5secs after applying the patch suggest over here https://www.fusonic.net/en/blog/3-steps-for-fast-entityframework-6.1-code-first-startup-performance/

    It worked and now it's 1.5 secs. We are still working on with pre-generated Views to make the LINQ calls under 1 sec.

    I download the patch from codeplex.com and compiled EF. Has this been patched in EF released by Microsoft? or any other alternatives?


    public class MyContextConfiguration : DbConfiguration { public MyContextConfiguration() string cachePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\YOUR_APP_NAME\EFCache\"; MyDbModelStore cachedDbModelStore = new MyDbModelStore(cachePath); IDbDependencyResolver dependencyResolver = new SingletonDependencyResolver(cachedDbModelStore); AddDependencyResolver(dependencyResolver); } private class MyDbModelStore : DefaultDbModelStore { private staticbool useCachedDbModelStore; // Note that you should only enable DbContextStore during normal run scenarios without migrations. Migrations are currently not supported and will crash. public static void Configure(booluseCachedDbModelStore{ MyContextConfiguration.useCachedDbModelStore = useCachedDbModelStore; } publicMyContextConfiguration() // CachedDbModel store wird derzeit nicht immer verwendet, da er z.b. bei Migrations derzeit noch nicht funktioniert (Exceptions im EF Code) if (useCachedDbModelStore) { MyDbModelStore cachedDbModelStore =new MyDbModelStore(MyContext.EfCacheDirPath); IDbDependencyResolver dependencyResolver = newSingletonDependencyResolver(cachedDbModelStore); AddDependencyResolver(dependencyResolver); } } private classMyDbModelStore : DefaultDbModelStore { public MyDbModelStore(string location) : base(location{} public overrideDbCompiledModel TryLoad(Type contextTypestring path = GetFilePath(contextType); if(File.Exists(path)) { DateTime lastWriteTime = File.GetLastWriteTimeUtc(path); DateTime lastWriteTimeDomainAssembly = File.GetLastWriteTimeUtc(typeof(TypeInYourDomainAssembly).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); } } } }

    • Moved by Bob Beauchemin Thursday, May 26, 2016 4:09 PM Moving to a more appropriate forum
    Thursday, May 26, 2016 2:20 PM

Answers

  • Hi ossprologix,

    >>I download the patch from codeplex.com and compiled EF. Has this been patched in EF released by Microsoft? or any other alternatives?

    It seems that Entity Framework 6 provides APIs to to generate views are on the System.Data.Entity.Core.Mapping.StorageMappingItemCollection class. You can retrieve a StorageMappingCollection for a Context by using the MetadataWorkspace of an ObjectContext. If you are using the newer DbContext API then you can access this by using the IObjectContextAdapter like below, in this code we have an instance of your derived DbContext called dbContext:

    var objectContext = ((IObjectContextAdapter) dbContext).ObjectContext; 
    var  mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace 
                                                                        .GetItemCollection(DataSpace.CSSpace);

    For more information, please refer to the following article

    https://msdn.microsoft.com/en-us/data/dn469601.aspx#code

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 27, 2016 7:02 AM