Entity Framework: Why is a property of type of an array of objects not stored in the database?

I have two objects in which one is related to each other. Example:

public class Question
{
    public int Id { get; set; }
    public string Text { get; set; }
    public Answer[] Answers { get; set; }
}

public class Answer
{
    public int Id {get; set; }
    public string Text { get; set; }
}

Using EF6 Code First, I installed this simple DbContext:

public class MyContext : DbContext
{
    public MyContext()
    {
        Database.SetInitializer<MyContext>(new DropCreateDatabaseAlways<MyContext>());
    }

    public DbSet<Question> Questions { get; set; }

    public DbSet<Answer> Answers { get; set; }
}

What I get in the database are two similarly structured tables (int PK column and varchar column), and there is no representation of the “one question has many answers” ​​relationship that I intended to use with the Question.Answers property.

Why doesn't the EF code first display the relationship and how can I fix it?

+4
source share
1 answer

Entity Framework . , ICollection<T> .

:

public class Question
{
    public int Id { get; set; }
    public string Text { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

Answers HashSet List:

this.Answers = new List<Answer>();
+3

Source: https://habr.com/ru/post/1540464/


All Articles