diff --git a/src/shared/Z.EF.Plus._Core.Shared/Model/DbContext.GetModel.cs b/src/shared/Z.EF.Plus._Core.Shared/Model/DbContext.GetModel.cs index 2822e54f..4d6fe35d 100644 --- a/src/shared/Z.EF.Plus._Core.Shared/Model/DbContext.GetModel.cs +++ b/src/shared/Z.EF.Plus._Core.Shared/Model/DbContext.GetModel.cs @@ -13,6 +13,8 @@ using System.Runtime.Caching; using Z.EntityFramework.Plus.Internal; using Z.EntityFramework.Plus.Internal.Core.Infrastructure; +using System.Linq; +using System.Data.Entity.Core.Objects; #if EF5 using System.Data.Metadata.Edm; @@ -28,6 +30,38 @@ internal static partial class InternalExtensions #if BATCH_DELETE || BATCH_UPDATE public static string STANDALONE_ID = Guid.NewGuid().ToString(); #endif + + internal static DbModelPlus GetModel(this IQueryable query) { + + var dbContext = query.GetDbContext(); + + if (dbContext == null) { + return query.GetObjectQuery().Context.GetModel(); + } + + return query.GetDbContext().GetModel(); + } + + internal static DbModelPlus GetModel(this ObjectContext context) { + + var cache = MemoryCache.Default; + +#if BATCH_DELETE || BATCH_UPDATE + var cacheName = "Z.EntityFramework.Plus.Model;" + STANDALONE_ID + ";" + context.GetType().FullName; +#else + var cacheName = "Z.EntityFramework.Plus.Model;" + context.GetType().FullName; +#endif + var model = (DbModelPlus)cache.Get(cacheName); + if (model == null) { + + var metadataworkspace = context.MetadataWorkspace; + model = Model.GetDatabaseFirst(context); + cache.Add(new CacheItem(cacheName, model), new CacheItemPolicy()); + } + + return model; + } + internal static DbModelPlus GetModel(this DbContext context) { var cache = MemoryCache.Default; diff --git a/src/shared/Z.EF.Plus._Core.Shared/Model/Model/Model.GetDatabaseFirst.cs b/src/shared/Z.EF.Plus._Core.Shared/Model/Model/Model.GetDatabaseFirst.cs index 3a717e69..2fa10055 100644 --- a/src/shared/Z.EF.Plus._Core.Shared/Model/Model/Model.GetDatabaseFirst.cs +++ b/src/shared/Z.EF.Plus._Core.Shared/Model/Model/Model.GetDatabaseFirst.cs @@ -3,13 +3,18 @@ // Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues // License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE // More projects: http://www.zzzprojects.com/ -// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. #if FULL || BATCH_DELETE || BATCH_UPDATE #if EF5 || EF6 using System; using System.Data.Entity; +using System.Data.Entity.Core.Metadata.Edm; +using System.Data.Entity.Core.Objects; +using System.IO; using System.Linq; +using System.Reflection; +using System.Xml; using System.Xml.Linq; using Z.EntityFramework.Plus.Internal.Core.Infrastructure; using Z.EntityFramework.Plus.Internal.Core.Mapping; @@ -43,6 +48,32 @@ internal static DbModelPlus GetDatabaseFirst(DbContext context) return GetDatabaseFirst(conceptualString, storageString, mappingString, modelSplit); } + public static DbModelPlus GetDatabaseFirst(ObjectContext context) { + + var modelSplit = "---zzz_multi_model_split_zzz---"; + var asm = context.GetType().Assembly; + + if(!asm.GetManifestResourceNames().Any(x => x.EndsWith(".ssdl"))) { + asm = context.GetType().BaseType.Assembly; + } + + var rn = asm.GetManifestResourceNames(); + var ssdl = rn.FirstOrDefault(x => x.EndsWith(".ssdl")); + var csdl = rn.FirstOrDefault(x => x.EndsWith(".csdl")); + var msl = rn.FirstOrDefault(x => x.EndsWith(".msl")); + + + + return GetDatabaseFirst(GetXml(asm,csdl), GetXml(asm,ssdl), GetXml(asm,msl), modelSplit); + } + + private static string GetXml(Assembly asm, string rn) { + using (var sr = new StreamReader(asm.GetManifestResourceStream(rn))) { + return sr.ReadToEnd(); + } + } + + public static DbModelPlus GetDatabaseFirst(string conceptualModel, string storageModel, string mappingModel, string modelSplit) { var model = new DbModelPlus(); @@ -135,4 +166,4 @@ public static DbModelPlus GetDatabaseFirst(string conceptualModel, string storag } #endif -#endif \ No newline at end of file +#endif