为简单起见,让我们考虑一个模仿StackOverflow问题的简单场景。
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public IEnumerable<Question> Questions { get; set; }
public IEnumerable<Vote> Votes { get; set; }
}
public class Question
{
public int Id { get; set; }
public string Content { get; set; }
public User User { get; set; } // the user to whom the question belongs
public int UserId { get; set; }
public IEnumerable<Vote> Votes { get; set; }
}
public class Vote
{
public bool IsUp { get; set; } // true = upvote or false = downvote
public User User { get; set; }// the user who votes a question
public int UserId { get; set; }
public Question Post { get; set; }// the question for which this vote is intended
public int QuestionId { get; set; }
}
如何确保没有用户对同一个问题投票两次或更多次?我是新手!如果我的实体模型实际上太糟糕了,欢迎任何建议或改进!
答案 0 :(得分:1)
您需要将它们按列顺序放置,然后SQL Server将映射它 这是一个例子
public class Vote
{
[Key, Column(Order = 0)]
public string UserID { get; set; }
[Key, Column(Order = 1)]
public int QuestionID { get; set; }
}