首页 > 解决方案 > 尽管使用 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);
    }
}

标签: c#entity-framework-6

解决方案


推荐阅读