Now I am building a mini blog. when user create a post I want to insert user id from AspNetUsers in post table as a foreign key here the post model, so can anyone tell me the steps to make it.
public class Post
{
[Required]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Content { get; set; }
[Required]
public string Path { get; set; }
[Required]
public DateTime PostDate { get; set; }
public ApplicationUser User { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
ApplicationUserin thePosttable. For example; if you want to see all Posts by a particular user, you simply callcontext.Posts.Where(p => p.User.Id == 5)public ApplicationUser User { get; set; }change it topublic int ApplicationUserId { get; set; }.. I say instead because if you have both, then it would be redundant. Then you will need to do some additional Fluent API code to map the foreign key to the User table. If you keep it as is, EF does all the work for you without any additional overhead.