c# - 尽管使用 Fluent API 对其进行了映射,但类仍被映射
问题描述
我正在尝试添加不应映射到数据库模型的类型的属性。为什么 Fluent API 的 Ignore 方法与 NotMapped 属性的行为不同?NotMapped 属性的实际等效项是什么?
我已经阅读了几篇声称 NotMapped 属性的实际等价物是 Ignore 方法的帖子和文章。不幸的是,如果我不添加提到的属性,我会收到错误:
System.Collections.Generic.KeyNotFoundException:给定的键不在字典中。在 System.ThrowHelper.ThrowKeyNotFoundException() 在 System.Collections.Generic.Dictionary`2.get_Item(TKey key) 在 System.Web.Http.OData.Builder.EdmTypeBuilder.ReorderDependentProperties(NavigationPropertyConfiguration navProperty) 在 System.Web.Http.OData .Builder.EdmTypeBuilder.CreateNavigationProperty(EntityTypeConfiguration config) 在 System.Web.Http.OData.Builder.EdmTypeBuilder.GetEdmTypes() (...)
我想避免在我的域模型上使用任何属性,所以我试图找到基于 Fluent API 的解决方案。
“Common.Model.SomeComplexModel”类不是数据库实体。
public class Vehicle
{
public int Id { get; set; }
public string Name { get; set; }
// An error occures if I remove following attribute
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public Common.Model.SomeComplexModel AdditionalData { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<Common.Model.SomeComplexModel>();
modelBuilder.Configurations.Add(new VehicleConfiguration());
(...)
}
internal class VehicleConfiguration : EntityTypeConfiguration<Vehicle>
{
internal VehicleConfiguration()
{
(...)
Ignore(v => v.AdditionalData);
}
}
解决方案
推荐阅读
- android - Firebase 电话已验证
- swift - 无法使用 .Type swift 访问值
- r - 以两个日期之间的最小差异为条件的数据框子集
- sql - 在窗口函数中计数不同
- django - 如何阻止来自未知域/IP 对我的 REST API 的调用?
- javascript - 调用 post 方法未传递字符串数组
- python - 使用 mplcursors 为同一图上的多个数据集添加标签
- google-chrome - 有没有办法将 chrome 扩展的 `chrome.tabs` 映射到 puppeteer 的 browser.pages()?
- javascript - 创建服务。从视图中提取逻辑到纯 React 中的单独文件
- python - raise RuntimeError('You must compile your model before using it.') in Keras at fit.generator