1

As I mentioned here, I changed the structure of my ASP.NET MVC 5 app like the following:

class Post {
    [Key]
    public int Id {get;set;}
    public DateTime CreationDate {get;set;}
    [Required]
    public virtual string Content {get;set;}
    public virtual Thread RelatedThread {get;set;}
}

class Thread : Post {
    public int ViewsCount {get;set;}
    [NotMapped]
    public override Thread RelatedThread {get;set;}
}

Because a thread consists of a post as startpost and have some additional attributes like the views. But the RelatedThread attribute of the post will destroy my data-structure, so I want to overwrite it and let EF ignore it by using the NotMapped attribute (see code above).

This does not work, I get the following error (translated):

The ignore-method cannot be used for the entity 'RelatedThread' in the model 'Thread' because he's inheriting from 'Post', in that the attribute is assigned. To exclude the attribute from the model, use the NotMapped attribute or the ignore-method for the base-type.

I also tried to exclude the attribute using fluent api in the OnModelCreating method of my DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Thread>().Ignore(thread => thread.RelatedThread);
}

But nothing changes, I get the same exception again - Although I used both methods which are recommended in the exception. Why is this not working?

Community
  • 1
  • 1
Lion
  • 16,606
  • 23
  • 86
  • 148
  • Did you try commenting out the NotMapped attr in the Thread class whilst using the OnModelCreating as you have above? – Spock Oct 27 '15 at 03:03
  • Yes, I tried both solutions alone (NotMapped-Attribute without .Ignore in OnModelCreating and .Ignore without the NotMapped-Attribute), botn didn't work and throw the same exception. – Lion Oct 27 '15 at 09:55

0 Answers0