当实体框架保存更改时,我开发了重置缓存的方法。我在objectContext_SavingChanges
private void objectContext_SavingChanges(object sender, EventArgs e)
{
if (_cacheProvider != null)
{
var objectContext = (this as IObjectContextAdapter).ObjectContext;
var entries =
objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added);
var result = new List<string>();
if (entries != null && entries.Any())
{
foreach (var entry in entries.Where(entry => !entry.IsRelationship))
{
var entity = entries.Select(r => r.Entity).FirstOrDefault();
if (entity != null)
{
var genericTypeName =
typeof(List<>).MakeGenericType(ObjectContext.GetObjectType(entity.GetType()))?.ToString();
_cacheProvider.ResetCache(genericTypeName);
}
}
foreach (var entry in entries.Where(entry => entry.IsRelationship))
{
var set = entry.EntitySet as AssociationSet;
if(set !=null)
{
var firstEntitySet = set.AssociationSetEnds[0].EntitySet;
var secondEntitySet = set.AssociationSetEnds[1].EntitySet;
var firstEntitySetName = firstEntitySet.ElementType.FullName + "," + Assembly.GetExecutingAssembly().FullName;
var secondEntitySetName = secondEntitySet.ElementType.FullName + "," + Assembly.GetExecutingAssembly().FullName;
var firstGenericTypeName =
typeof(List<>).MakeGenericType((Type.GetType(firstEntitySetName)))?.ToString();
var secondGenericTypeName =
typeof(List<>).MakeGenericType((Type.GetType(secondEntitySetName)))?.ToString();
_cacheProvider.ResetCache(firstGenericTypeName);
_cacheProvider.ResetCache(secondGenericTypeName);
}
}
}
}
}
它适用于实体对象状态条目的条目,但不适用于关联集。我需要为关系的其他方面的两个实体重置缓存。 AssociationSet有&#34; FullName&#34;属性,但此属性不包含类型的真实名称。它包含类型和程序集名称的简称,但不包含解决方案中的完整命名空间。
例如,类型在解决方案DataAccess.Entities.CachedEntity
中具有真实全名,命名空间:DataAccess.Entities
,短名称:CachedEntity
。但关联集中的全名将为DataAccess.CachedEntity
。
我可以为关系的每一端获得类型的全名吗?
答案 0 :(得分:1)
我决定这个任务:
<div id="MediaHTML">
<div id="MediaInnholdFortegnelse">
<h1>MEDIA KATEGORIER</h1>
<button type="button", id="MIF-Bilder">
<div class="MIF-ButtonContent">
<i class="material-icons md-c">arrow_drop_down</i>
<p>Bilder</p>
</div>
</button>
<div id="MIF-Bilder-Content", class="MIF-Content"></div>
<button type="button", id="MIF-Lydfiler">
<div class="MIF-ButtonContent">
<i class="material-icons md-c">arrow_drop_down</i>
<p>Lydfiler</p>
</div>
</button>
<div id="MIF-Lydfiler-Content", class="MIF-Content"></div>
<button type="button", id="MIF-Videoer">
<div class="MIF-ButtonContent">
<i class="material-icons md-c">arrow_drop_down</i>
<p>Videoer</p>
</div>
</button>
<div id="MIF-Videoer-Content", class="MIF-Content"></div>
<button type="button", id="MIF-Skjema">
<div class="MIF-ButtonContent">
<i class="material-icons md-c">arrow_drop_down</i>
<p>Skjema</p>
</div>
</button>
<div id="MIF-Skjema-Content", class="MIF-Content">
</div>
</div>
我们可以在EDM中使用CLR类型的名称空间 var clrTypeNamespace = @"http://schemas.microsoft.com/ado/2013/11/edm/customannotation:ClrType";
var firstEdmType = set.AssociationSetEnds[0]?.EntitySet?.ElementType;
var secondEdmType = set.AssociationSetEnds[1]?.EntitySet?.ElementType;
var firstType = firstEdmType?.MetadataProperties.SingleOrDefault(p => p.Name == clrTypeNamespace)?.Value as Type;
var secondType = secondEdmType?.MetadataProperties.SingleOrDefault(p => p.Name == clrTypeNamespace)?.Value as Type;
,并在clrTypeNamespace
此外,我们可以为每个关系的集合使用导航属性。但是,当关系的两侧存在导航属性时,此解决方案可用于情况。
MetadataProperties