ApplicationUser我将创建一个在和自己创建的类之间具有多对多关系的表,名为Topic. 我将用于保存用户对某个主题的赞成票和反对票的表格,这样用户就不能多次投票给一个主题。所以我的想法是使用如下所示的表格(⚿ 是表格的主键):
╔══════════════════════╦════════════════════╦═════════╦════════════════════════╗
║ UserId ⚿ ║ TopicId ⚿ ║ Vote ║ Time ║
╠══════════════════════╬════════════════════╬═════════╬════════════════════════╣
║ x ║ 1 ║ up ║ 2016/02/02 15:00:00 ║
║ y ║ 2 ║ up ║ 2016/02/01 15:00:00 ║
║ y ║ 1 ║ down ║ 2016/01/01 14:00:00 ║
╚══════════════════════╩════════════════════╩═════════╩════════════════════════╝
↓ ↓ ↓ ↓
Must reference to Must reference to Other information about the vote
the table where the the table where my
users are stored topic are stored
如何使用身份框架将此结构迁移到数据库?我也使用代码优先的工作方式。
您可以在下面找到我的Topic课程和扩展到该课程的其他课程。类中的代码ApplictionUser不变。
public class Topic
{
public int TopicId { get; set; }
public bool Deleted { get; set; }
public string UserId { get; set; }
public ApplicationUser User{ get; set; }
public string Text { get; set; }
public DateTime Creation { get; set; }
public List<Vlag> Flags { get; set; }
public int? BlogId { get; set; }
public Blog Blog { get; set; }
public int? ReactionId { get; set; }
public Reaction Reaction { get; set; }
}
public class Blog: Topic, IVote
{
public int Id { get; set; }
public int CategorieId { get; set; }
public Categorie Categorie { get; set; }
public string Name { get; set; }
public int Down { get; set; }
public int Up { get; set; }
public int CalculateTotal()
{
return Up - Down;
}
}
public class Reactie : Topic, IVote
{
public int Id { get; set; }
public int Down { get; set; }
public int Up{ get; set; }
public Blog OwnerBlog { get; set; }
public int OwnerBlogId { get; set; }
public int CalculateTotal()
{
return Up - Down;
}
}
public interface IVote // used for holding the number of up and down votes for a blog
// or reaction.
{
int Up { get; set; }
int Down { get; set; }
int CalculateTotal();
}