0

I got these error on those calls. I must be doing something wrong :-) but where?

On my controller I have this:

var faqs = _context.Faqs.Include(c=>c.Categories).ThenInclude(c=>c.category);

var forums = _context.Forums.Include(c => c.Categories).ThenInclude(c => c.category);

Errors:

Additional information: Invalid column name 'FaqId'.

Additional information: Invalid column name 'ForumId'.

Code:

public class Category
{
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    [Required()]
    public string Name { get; set; }

    [Required()]
    public Int64 ApplyTo { get; set; }=0;

    [Required()]
    public Int64 EnumNumber { get; set; } = 0; 
}

public class CategoryObject 
{
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// Refers to class object like FAQs, Forum
    /// </summary>        
    public string ObjectId { get; set; }

    public virtual Category category { get; set; }
    public string CategoryId { get; set; } 
}

public class Faq
{
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString(); 

    [Required()]
    public string Name { get; set; }         
    [Required()]
    public string Answer { get; set; }

    /// <summary>
    /// List of all categories this FAQ belongs
    /// </summary>
    public virtual ICollection<CategoryObject> Categories { get; set; } 
}

public class Forum
{
    [Key]
    public string Id { get; set; } = Guid.NewGuid().ToString();         

    [Required()]
    public override string Name { get; set; }

    public string Description { get; set; } = "";

    /// <summary>
    /// List of all categories this Forum belongs
    /// </summary>
    public virtual ICollection<CategoryObject> Categories { get; set; }
}

The all idea is that I have an object flagged with some categories and would like to pull all the categories that the object has. In this case, FAQ and Forum plus others could have some categorization. There might be a better way? Open to suggestions as well.

Thanks

Nicolas
  • 1
  • 1
  • 5
  • Looks like EF 7 / EF Core? – Robert McKee Dec 13 '16 at 19:18
  • Look at the [entityframeworktutorial](http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx) site and clarify your question further – Jeroen Heier Dec 13 '16 at 19:34
  • For me in `Category` missing foreign key to `Faq` and `Forum` – Perdido Dec 13 '16 at 19:46
  • Your database and entity model are out of sync. And you can't have a field that *Refers to class object like FAQs, Forum*. – Ivan Stoev Dec 13 '16 at 19:47
  • But If I only used for example the FAQ and remove `public virtual ICollection Categories { get; set; }` from the _forum_ it works fine. So what should I do different to achieve my goal. There are 2-3, 4 table/class whose going to be using the _CategoryObject_ mapping – Nicolas Dec 13 '16 at 20:20

0 Answers0