我有2种型号,卡片和卡座。用户将新卡添加到集合(索引页面),并且用户可以创建新卡组。我如何与模型一起使用,以允许用户将收集的卡片放到卡座上?
我的模特看起来如何?我假设我需要将卡绑定到卡座等,但不确定如何。有人可以澄清我需要做什么以及如何做吗?
我已经创建了2个模型卡座和卡,并使卡座成为卡模型中引用的数据类型。
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甲板建造者。
预期结果是,收集到的卡片随后插入卡座。想象一下,在查看Deck索引时,它将填充用户选择的集合中的卡片。
答案 0 :(得分:0)
编辑:
添加“中间”实体
public class CardsInDeck
{
public int DeckId { get; set; }
public int CardId { get; set; }
}
在卡座模型中添加“中间”的集合
public class Deck
{
public ICollection<CardsInDeck> Cards { get; set; }
}
在卡片模型中添加“中间”的集合
public class Deck
{
public ICollection<CardsInDeck> Decks { get; set; }
}
Here you have basic many to many relation。基本上是两个一对多的关系(Deck-Middle实体,Card-Middle实体)。
答案 1 :(得分:0)
使用Card和Deck的外键作为关系的主键,这样的事情可能会起作用
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; }
}