实体框架5不能对属性使用Ignore方法 [英] Entity Framework 5 Cannot use Ignore method on the property

查看:286
本文介绍了实体框架5不能对属性使用Ignore方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎耗尽了这个错误的所有解决方法:
http:// entityframework。 codeplex.com/workitem/481

I have almost exhausted all of the workarounds for this bug: http://entityframework.codeplex.com/workitem/481

请有人指出我的方向正确。我已经完成了以下操作:

Please can someone point me in the right direction. I have done the following:

步骤1:
从所有实体和基类的属性中删除NotMapped属性。在我的解决方案中没有没有NotMapped属性。

Step 1: Remove the NotMapped attribute from the properties in all entities and base classes. There are no NotMapped attributes left in my solution at all.

步骤2:
在所有实体的所有属性上使用OnModelCreating方法中的ignore方法(严重)我花了几天时间和我拥有的实体数量相同)

Step 2: Use the ignore method in the OnModelCreating method for all properties on all entities (Seriously this took me a couple days with the sheer number of entities I have)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
      modelBuilder.Entity<MyEntity>().Ignore(p => p.MyProperty);
}

但是,当我运行时,我收到以下错误:

However, when I run I get the following error:

您不能在Namespace.MyEntity类型的属性MyProperty上使用Ignore方法,因为此类型从此属性映射的类型MyBaseEntity继承。您的模型,在基类型上使用NotMappedAttribute或Ignore方法。

"You cannot use Ignore method on the property 'MyProperty' on type 'Namespace.MyEntity' because this type inherits from the type 'MyBaseEntity' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type."

我还需要做什么?它绝对没有映射,因为我在模型构建器中忽略它!对吧!

What else do I need to do? It is definitely not mapped because I've ignored it in the model builder!!! Right!?

帮助!!!

推荐答案

(转发自 EF5代码 - 您不能在属性上使用Ignore方法

在我的情况下,当使用Code First (EF6)在现有数据库上,我创建了一些基类来处理常见属性,如 ID

In my case, when using Code First (EF6) on an existing database, I created some base classes to handle the common properties like ID.

(注意:以下内容在 OnModelCreating(DbModelBuilder mb)方法之内)

(Note: the following are inside the OnModelCreating(DbModelBuilder mb) method)

然后我需要完全忽略基础类

mb.Ignore(new[] {
    typeof(BaseClassA),
    typeof(BaseClassB)
});

然后,有点违反直觉,我需要注册基本模型属性:

Then, somewhat counterintuitively, I needed to register the base model properties with:

mb.Entity<BaseClassA>().HasKey(m => m.ID);
mb.Entity<BaseClassB>().Whatever...

类需要忽略其中一个基本属性(称为 NormallyNotIgnored )。我使用 EntityTypeConfiguration ,但我认为你可以用常规流利来做同样的事情:

One of my derived classes needed to ignore one of the base properties (call it NormallyNotIgnored). I used EntityTypeConfiguration, but I assume you could do the same with regular Fluent:

mb.Entity<DerivedClassB1>().Ignore(m => m.NormallyNotIgnored);

至少已编译/迁移(使用 -IgnoreChanges ,因为表已经存在)并解决了有问题的错误。

This at least has compiled/migrated (with -IgnoreChanges on the migration, since the tables already exist) and resolved the error in question.

这篇关于实体框架5不能对属性使用Ignore方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆