M
M
Matsun2017-02-28 13:10:43
ASP.NET
Matsun, 2017-02-28 13:10:43

Entity Framework Code First - why when creating a one to many relationship, 2 columns are created in the table?

I use Entity Framework Code First technology when designing a database for my blog.
At the moment, I am writing the essence of the article (Article) and comments (Comment).
The Article entity refers to Comment - one to many.
Why, when creating a database, the framework creates 2 columns in the Comment table at once:
Comment_ID (why?)
Article_ID (everything is clear with this one).
92c4f01d3ef5.jpg
Moreover, in the Comment_ID column, all NULL values

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor P., 2017-02-28
@Jeer

Show some code? I think I wrote something wrong

public class Article
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }

       ...

        public virtual ICollection<Comment> comm { get; set; }
    }

public class Comment
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public Guid ArtId { get; set; }
        
       ...

        [ForeignKey("ArtId")]
        public virtual Article art { get; set; }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question