![](/img/trans.png)
[英]EF Core property value conversion not working in a derived entity configuration
[英]How to exclude a property of all entity types in EF Core configuration?
我的所有模型都有一個名為RelatedItems
的動態屬性。 我想動態忽略所有實體類型的這些屬性。
因此我創建了一個基本的DatabaseContext
來集中這個約定。
public abstract DatabaseContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var allEntities = modelBuilder.Model.GetEntityTypes();
foreach (var entity in allEntities)
{
entity.Ignore("RelatedItems");
}
}
}
但這給了我這個錯誤:
DatabaseContext.cs(23,24): 錯誤 CS1061: 'IMutableEntityType' 不包含
'Ignore' 的定義並且沒有可訪問的擴展方法 'Ignore' 接受第一個
可以找到類型為“IMutableEntityType”的參數
如何忽略所有模型上的屬性?
// Get all entities and filter the ones who has the RelatedItems properties
// This filter is not required, but for safety purpose I recomend to use it
// otherwise you could end up getting some weird errors during migration scaffolding
// In my case, without it, the scaffold command alarmed about an Enum that is not even related
var allEntities = modelBuilder.Model.GetEntityTypes()
.Where(x => x.GetProperties().Count(x => x.Name == "RelatedItems") > 0);
foreach (var entity in allEntities)
{
// Ignore property
modelBuilder.Entity(entity.Name).Ignore("RelatedItems");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.