Entity Framework 5 Cannot use Ignore method on the property

我与影子孤独终老i 提交于 2019-12-11 08:55:08

问题


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:

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.

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:

"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!?

Help!!!


回答1:


I know this question is old, but I couldn't find explicit instructions for my scenario anywhere.

I was just going back and forth between configurations that wouldn't create databases and models that configurations that created databases, but wouldn't save entities!

And of course, the very exception that you are reporting now was one of the most irritating to deal with!

You may have a discovery issue. I'd need to see more of your model to help you determine where the issue is.

What does 'Namespace.MyEntity' look like? Is it participating in a TPT inheritance mapping strategy?

If so, do you have any 1|1 relationships (<--not really possible) or 1|* relationships with other TPT entities?

I'm completely guessing here, but this took some trial and error to figure out, so I'll post it to help anyone that searches for your error and [-- TPT | Table Per Type | Required | Relationship | Parent | Child | Shared | Base | Abstract --]

In my scenario, when I had the need for a relationship that was modelBuilder.Entity().HasRequired(x => x.TPT_Derived_Parent_Class).WithOptional(); I was forced to make the TPT_Derived_Child_Class inherit from a separate project base class than the TPT_Derived_Parent_Class

I have found, in my solution, that it is very important that entities are discovered by Code First in the correct order when you have TPT derived classes with non-nullable fields referencing classes derived from a different TPT parent class

I have found it impossible (in my setup) to have a generic project base class that all other classes (abstract or concrete) inherit from when I am using TPT for the derived (second level) base classes AND the concrete classes that derive from the second level base classes have required foreign key relationships with each other.

For example, this doesn't work for me:

public abstract ProjectBaseClass : IProjectBase
{
      [Key]
      public int ProjectClassesId {get;set;}
}

[Table("TPT_BaseClass1")]
public abstract TPT_Specialized_Base_Class1 : ProjectBaseClass
{
      //...specialized stuff in here
}
[Table("TPT_BaseClass2")]    
public abstract TPT_Specialized_Base_Class2: ProjectBaseClass
{
     //...specialized stuff in here
}

[Table("ConcreteChild")]
public TPT_Child_Concrete_Class : TPT_Specialized_Base_Class1
{
      public int TPT_Parent_Concrete_Class_KeyId {get;set;};

      [ForeignKey("TPT_Parent_Concrete_Class_KeyId ")]
      TPT_Parent_Concrete_Class ParentSpecializedClass {get;set;};

}

[Table("ConcreteParent")]
public TPT_Parent_Concrete_Class : TPT_Specialized_Base_Class2
{
      //optional relationship 
      public int? TPT_Child_Concrete_Class_KeyId {get;set;};

      [ForeignKey("TPT_Child_Concrete_Class_KeyId")]
      TPT_Child_Concrete_Class ChildSpecializedClass {get;set;};

}


public projectContext: DbContext
{
     public DbSet<TPT_Specialized_Base_Class1> 
     public DbSet<TPT_Specialized_Base_Class2> 


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    // There is no configuration I could find that would make the model above work!

    // However, if you make each TPT_Specialized_Base_Class inherit from different
    // ProjectBaseClass like:
    public ProjectBaseClass1 : IProjectBase
    public ProjectBaseClass2 : IProjectBase


    public TPT_Specialized_Base_Class1 : ProjectBaseClass1 {...}
    // and 
    public TPT_Specialized_Base_Class2 : ProjectBaseClass2 {...}

    // or, more sensible... 
    public TPT_Specialized_Base_Class1 : IProjectBase
    // and 
    public TPT_Specialized_Base_Class2 : IProjectBase

    // then you can do the following, making sure you *discover* the child TPT base 
    // class first

    modelBuilder.Entity<TPT_Specialized_Base_Class1>() //this one is inherited by the derived class that has the *required* reference to a TPT derived parent
         .Ignore(x => x.PropertyNamedInErrorMessage);

    modelBuilder.Entity<TPT_Specialized_Base_Class2>();
         .Ignore(x => x.PropertyNamedInErrorMessage);

    // when I flipped the order of the classes above, it could not determine the 
    // principal end of the relationship, had a invalid multiplicity, or just wouldn't 
    // save...can't really remember what it was crying about...

    modelBuilder.Entity<TPT_Child_Concrete_Class>()
     .HasRequired(x => x.TPT_Parent_Concrete_Class ).WithOptional(); 
          & ProjectBaseClass2
    }
}



回答2:


( reposting from EF5 code first - You cannot use Ignore method on the property )

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

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

I then needed to ignore the base classes entirely with:

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...

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);

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



来源:https://stackoverflow.com/questions/15525696/entity-framework-5-cannot-use-ignore-method-on-the-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!