So I’m going to use the cliché example of a blogging database model with posts and comments.
public class Post {
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment {
public int Id { get; set; }
public string Body { get; set; }
public virtual Post Post { get; set; }
}
Of course, Post has a one-to-many relationship with Comment.
If I let Entity Framework create the database for me, it will add a Post_Id column to the Comments table. I would like to have the column named PostId instead, but I don’t want to add a PostId property to my Comments class. Is there a way to achieve this via DataAnnotations, DbModelBuilder, migrations, something else I don’t know about?
I apologize if this has already been answered, but I haven’t been able to find it on SO or even Google. Thanks.
And right after I post this, I find the answer.
Declare this method on the class derived from DbContext.