C# 从代码生成映射视图-EF6
我试图在我拥有500多个实体的庞大代码库中实现链接文章中提到的策略,以提高性能。我被以下问题困住了 发生System.Data.Entity.Core.EntityCommand编译异常C# 从代码生成映射视图-EF6,c#,asp.net,performance,entity-framework-6,C#,Asp.net,Performance,Entity Framework 6,我试图在我拥有500多个实体的庞大代码库中实现链接文章中提到的策略,以提高性能。我被以下问题困住了 发生System.Data.Entity.Core.EntityCommand编译异常 HResult=0x8013193B消息=准备时出错 命令定义。有关详细信息,请参见内部异常。 源=堆栈跟踪: 内部异常1:MappingException:当前模型不再存在 匹配用于预生成映射视图的模型,如图所示 由 视图浏览实体设置3193163CE5583733333438629C877839AE9E7B
HResult=0x8013193B消息=准备时出错 命令定义。有关详细信息,请参见内部异常。
源=堆栈跟踪: 内部异常1:MappingException:当前模型不再存在 匹配用于预生成映射视图的模型,如图所示 由 视图浏览实体设置3193163CE5583733333438629C877839AE9E7B7494500B6FD275844CDA6D343.MappingHashValue 财产。预生成的映射视图必须使用 如果在运行时生成映射视图,则删除当前模型或 应该改用。看见 有关 实体框架映射视图 这是我试过的。在我可能成为牺牲品的地方,原始文章中有一些关于如何实现它的空白 步骤1:我创建了一个扩展DBMappingViewCache的类
public class EFDbMappingViewCache : DbMappingViewCache
{
protected static string _mappingHashValue = String.Empty;
public override string MappingHashValue
{
get
{
return GetCachedHashValue();
}
}
public override DbMappingView GetView(EntitySetBase extent)
{
Dictionary<string, string> dict = GetMappedViewFromCache();
if (extent == null)
{
throw new ArgumentNullException("extent");
}
if(dict.ContainsKey(extent.Name))
{
return new DbMappingView(dict[extent.Name]);
}
return null;
}
public static string GetCachedHashValue()
{
string cachedHash;
string path = HttpContext.Current.Server.MapPath(@"~\EFCache\MappingHashValue.txt");
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
using (var streamReader = new StreamReader(path, Encoding.UTF8))
{
cachedHash = streamReader.ReadToEnd();
}
return cachedHash;
}
public static void UpdateHashInCache(string hashValue)
{
string path = HttpContext.Current.Server.MapPath(@"~\EFCache\MappingHashValue.txt");
using (var streamWriter = new StreamWriter(path, false))
{
streamWriter.Write(hashValue);
}
}
private static void UpdateMappedViewInCache(Dictionary<EntitySetBase, DbMappingView> dict)
{
string path = HttpContext.Current.Server.MapPath(@"~\EFCache\MappingView.json");
Dictionary<String, String> stringDict = new Dictionary<string, string>();
foreach(var entry in dict)
{
stringDict[entry.Key.Name] = entry.Value.EntitySql.ToString();
}
var json = new JavaScriptSerializer().Serialize(stringDict);
using (var streamWriter = new StreamWriter(path, false))
{
streamWriter.Write(json);
}
}
private static Dictionary<String, string> GetMappedViewFromCache()
{
string path = HttpContext.Current.Server.MapPath(@"~\EFCache\MappingView.json");
var json = String.Empty;
using (var streamReader = new StreamReader(path, Encoding.UTF8))
{
json = streamReader.ReadToEnd();
}
Dictionary<String, string> mappedViewDict = new Dictionary<String, string>();
if (!String.IsNullOrEmpty(json))
{
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
mappedViewDict = ser.Deserialize<Dictionary<String, string>>(json);
}
return mappedViewDict;
}
public static void CheckAndUpdateEFViewCache()
{
using (var ctx = new CascadeTranscationsDbContext(DBHelper.GetConnString()))
{
var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace
.GetItemCollection(DataSpace.CSSpace);
string computedHashValue = mappingCollection.ComputeMappingHashValue();
string currentHashValue = GetCachedHashValue();
SetHashValue(computedHashValue);
if (computedHashValue != currentHashValue)
{
UpdateHashInCache(computedHashValue);
IList<EdmSchemaError> errors = new List<EdmSchemaError>();
Dictionary<EntitySetBase, DbMappingView> result = mappingCollection.GenerateViews(errors);
UpdateMappedViewInCache(result);
}
}
}
}
公共类EFDbMappingViewCache:DbMappingViewCache
{
受保护的静态字符串_mappingHashValue=string.Empty;
公共重写字符串映射HashValue
{
得到
{
返回GetCachedHashValue();
}
}
公共覆盖DbMappingView GetView(EntitySetBase范围)
{
Dictionary dict=GetMappedViewFromCache();
if(区段==null)
{
抛出新的异常(“范围”);
}
if(dict.ContainsKey(extent.Name))
{
返回新的DbMappingView(dict[extent.Name]);
}
返回null;
}
公共静态字符串GetCachedHashValue()
{
字符串缓存;
字符串路径=HttpContext.Current.Server.MapPath(@“~\EFCache\MappingHashValue.txt”);
如果(!File.Exists(path))
{
File.Create(path.Dispose();
}
使用(var streamReader=newstreamreader(路径,Encoding.UTF8))
{
cachedHash=streamReader.ReadToEnd();
}
返回cachedHash;
}
公共静态void UpdateHashInCache(字符串哈希值)
{
字符串路径=HttpContext.Current.Server.MapPath(@“~\EFCache\MappingHashValue.txt”);
使用(var streamWriter=newstreamwriter(路径,false))
{
streamWriter.Write(hashValue);
}
}
私有静态void updateMappedViewCache(字典目录)
{
string path=HttpContext.Current.Server.MapPath(@“~\EFCache\MappingView.json”);
字典stringDict=新字典();
foreach(dict中的var条目)
{
stringDict[entry.Key.Name]=entry.Value.EntitySql.ToString();
}
var json=new JavaScriptSerializer().Serialize(stringDict);
使用(var streamWriter=newstreamwriter(路径,false))
{
streamWriter.Write(json);
}
}
私有静态字典GetMappedViewFromCache()
{
string path=HttpContext.Current.Server.MapPath(@“~\EFCache\MappingView.json”);
var json=String.Empty;
使用(var streamReader=newstreamreader(路径,Encoding.UTF8))
{
json=streamReader.ReadToEnd();
}
Dictionary mappedViewDict=新字典();
如果(!String.IsNullOrEmpty(json))
{
var ser=new System.Web.Script.Serialization.JavaScriptSerializer();
mappedViewDict=ser.Deserialize(json);
}
返回mappedViewDict;
}
publicstaticvoidcheckandupdateefviewcache()
{
使用(var ctx=new cascadeTransactionsDBContext(DBHelper.GetConnString()))
{
var objectContext=((IObjectContextAdapter)ctx).objectContext;
var mappingCollection=(StorageMappingItemCollection)objectContext.MetadataWorkspace
.GetItemCollection(DataSpace.CSSpace);
string computedHashValue=mappingCollection.ComputeMappingHashValue();
字符串currentHashValue=GetCachedHashValue();
SetHashValue(计算的HashValue);
if(computedHashValue!=currentHashValue)
{
UpdateHashInCache(计算hashValue);
IList errors=新列表();
字典结果=mappingCollection.GenerateViews(错误);
UpdateMappedViewCache(结果);
}
}
}
}
我将hashvalue和生成的映射存储在一个文件中,并在GetView()方法中检索它
我公开了一个public CheckAndUpdateEFViewCache()方法,该方法将在调用时生成视图映射并存储在文件中
步骤2:从Global.asax文件应用程序_Start()方法调用CheckAndUpdateEFViewCache()
步骤3:在第一次调用上下文的文件中包含程序集。
[程序集:DbMappingViewCacheType(typeof(Models.Entities.MyDBContext)、typeof(EFDbMappingViewCache))]
我真的不确定这条装配线到底需要去哪里。链接中没有关于它的信息。第三步很有可能就是我出错的地方
有人能帮忙解决这个问题吗 我所面临的问题是,我已经使用EF工具生成了一个映射文件,并且该文件已注册。当我编写的配置试图再次注册时,EF抛出