我在理解 MVC4 简单 Web 应用程序中的对象关系映射时遇到了一点麻烦,其中有用户及其发布的评论。一个用户必须有很多评论。所以我在UsersContext
课堂上添加了public DbSet<UserWork> UserComments { get; set; }
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<UserWork> UserComments { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? UserComId { get; set; }
[ForeignKey("UserComId")]
public virtual UserComment UserComLog { get; set; }
}
public class UserComment
{
[Key]
public int UserComId{ get; set; }
public int UserId { get; set; }
public string Comments{ get; set; }
public DateTime postDate{get;set}
}
我现在一直在意识到每天发布的所有评论是如何存储的,以便我以后可以进行查询,例如SELECT * FROM UserComment Inner join UserProfile ON UserComment.UserId=UserProfile.UserId WHERE postDate BETWEEN (...) AND (...)