gpt4 book ai didi

c# - 从代码生成映射 View - EF6

转载 作者:太空宇宙 更新时间:2023-11-03 14:52:23 25 4
gpt4 key购买 nike

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

我正在尝试在我拥有超过 500 个实体的庞大代码库中实现链接文章中提到的策略,以提高性能。我遇到以下问题。

System.Data.Entity.Core.EntityCommandCompilationException occurred
HResult=0x8013193B Message=An error occurred while preparing the command definition. See the inner exception for details.
Source= StackTrace:

Inner Exception 1: MappingException: The current model no longer matches the model used to pre-generate the mapping views, as indicated by the ViewsForBaseEntitySets3193163ce55837363333438629c877839ae9e7b7494500b6fd275844cda6d343.MappingHashValue property. Pre-generated mapping views must be either regenerated using the current model or removed if mapping views generated at runtime should be used instead. See http://go.microsoft.com/fwlink/?LinkId=318050 for more information on Entity Framework mapping views.

这是我尝试过的。原始文章中有一些关于如何实现它的空白,我可能会成为猎物。

第 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);
}
}
}


}

我已将哈希值和生成的映射存储在一个文件中,并在 GetView() 方法中检索它。

我公开了一个公共(public) CheckAndUpdateEFViewCache() 方法,该方法将在调用时生成 View 映射并将其存储在文件中。

第 2 步:从 Global.asax 文件中调用 CheckAndUpdateEFViewCache() Application_Start() 方法。

第 3 步:在首次调用上下文的文件中包含程序集。[程序集:DbMappingViewCacheType(typeof(Models.Entities.MyDBContext),typeof(EFDbMappingViewCache))]

我真的不确定这条流水线实际需要去哪里。链接中没有关于它的信息。 Step3 很有可能是我出错的地方。

有人可以帮忙解决这个问题吗?

最佳答案

我遇到的问题是因为我已经有一个使用 EF 工具生成的映射文件并且已注册。当我编写的配置尝试再次注册时,EF 抛出了一个错误。

此外,我想补充一点,缓存数据库模型存储将性能提高了几倍,我最终在我的项目中只使用了它。 Link to Cached DB model store usage

关于c# - 从代码生成映射 View - EF6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51195892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com