我如何连接我的两个模型以允许将卡片输入一副牌?

How do i connect my two models to allow cards to be entered into a deck?

我有2个模型,card和deck。用户将新卡添加到 collection(索引页),用户可以创建新卡组。我如何使用我的模型来允许用户将 collection 中的卡片放入牌组?

我的模型看起来怎么样?我假设我需要将卡片绑定到甲板等,但不确定如何。有人可以澄清我需要做什么以及如何做吗?

我创建了 2 个模型 deck 和 card,并在卡片模型中引用了 deck a data-type。

public class Card
{
    public int Id { get; set; }
    public string Name { get; set;  }
    public string Attribute { get; set; }
    public int Level { get; set; }
    public string Type { get; set; }
    public int ATK { get; set; }
    public int DEF { get; set; }
    public Deck Deck {get; set;}
    public int Deck {get; set;}
}

public class Deck
{  
    public int DeckId {get; set;}
    public string DeckName {get; set;}  
}

这是一个 Yu-Gi-Oh 牌组构建器。

预期结果是,来自 collection 的卡片随后被插入卡片组。想象一下,当查看 Deck 索引时,它将填充来自 collection 用户选择的卡片。

编辑:

添加'middle'实体

public class CardsInDeck
{  
    public int DeckId { get; set; }
    public int CardId { get; set; }
}

将 'middle' 的集合添加到牌组模型中

public class Deck
{  
    public ICollection<CardsInDeck> Cards { get; set; }
}

将 'middle' 的集合添加到卡片模型中

public class Deck
{  
    public ICollection<CardsInDeck> Decks { get; set; }
}

Here you have basic many to many relation。基本上是 2 个一对多关系(Deck-Middle 实体,Card-Middle 实体)。

像这样的东西可以工作,同时使用 Card 和 Deck 的外键作为 Relationships 的主键

 public class Card
        {
            public Card()
            {
                DeckCardRelationships = new HashSet<DeckCardRelationship>();
            }
            public int Id { get; set; }
            public string Name { get; set; }
            public string Attribute { get; set; }
            public int Level { get; set; }
            public string Type { get; set; }
            public int ATK { get; set; }
            public int DEF { get; set; }

            public virtual Deck Deck { get; set; }
            public virtual ICollection<DeckCardRelationship> DeckCardRelationships { get; set; }


        }

        public class DeckCardRelationship
        {
            public int CardId { get; set; }
            public int DeckId { get; set; }

            public virtual Card Card { get; set; }
            public virtual Deck Deck { get; set; }
        }

        public class Deck
        {
            public Deck()
            {
                DeckCardRelationships = new HashSet<DeckCardRelationship>();
            }

            public int Id { get; set; }
            public string DeckName { get; set; }


            public virtual ICollection<DeckCardRelationship> DeckCardRelationships { get; set; }
        }