0

Я создам таблицу со многими для многих отношений между ApplicationUser и собственным классом с именем Topic. Таблица, которую я буду использовать для хранения голосов вверх и вниз от пользователя для темы, чтобы пользователь не мог проголосовать несколько раз за тему. Так что моя идея заключается в том, чтобы использовать таблицу, как показано ниже (& # x26BF, являются первичными ключами таблицы):Перемещение кода для таблицы со многими отношениями и дополнительными столбцами

╔══════════════════════╦════════════════════╦═════════╦════════════════════════╗ 
║  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(); 
} 

ответ

0

Я нашел способ сделать это:

Table name: Votes 
╔═════════════════╦══════════════════════╦════════════════════╦═════════╦════════════════════════╗ 
║  VoteId ⚿ ║  UserId ↗  ║  TopicId ↗  ║ Vote ║   Time   ║ 
╠═════════════════╬══════════════════════╬════════════════════╬═════════╬════════════════════════╣ 
║ 1    ║ x     ║ 1     ║ up  ║ 2016/02/02 15:00:00 ║ 
╠═════════════════╬══════════════════════╬════════════════════╬═════════╬════════════════════════╣ 
║ 2    ║ y     ║ 2     ║ up  ║ 2016/02/01 15:00:00 ║ 
╠═════════════════╬══════════════════════╬════════════════════╬═════════╬════════════════════════╣ 
║ 3    ║ y     ║ 1     ║ down ║ 2016/01/01 14:00:00 ║ 
╚═════════════════╩══════════════════════╩════════════════════╩═════════╩════════════════════════╝ 
     ↓     ↓      ↓    ↓     ↓   
    New primary key  References to   References to  Other information about the vote 
    of the table the table where the the table where my          
         users are stored  topic are stored  

Легенда:

  • ⚿ является первичным ключом
  • ↗ ключи forgain
Смежные вопросы