Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for ObjectContext #194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/shared/Z.EF.Plus._Core.Shared/Model/DbContext.GetModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<T>(this IQueryable<T> query) {

var dbContext = query.GetDbContext();

if (dbContext == null) {
return query.GetObjectQuery<T>().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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -135,4 +166,4 @@ public static DbModelPlus GetDatabaseFirst(string conceptualModel, string storag
}

#endif
#endif
#endif