我有一个包含三个表Player,Game和Role的数据库。这三个表之间的关系通过另一个名为PlayerGameRole的表显示,该表包含三个表的ID作为外键,也是PlayerGameRole表的主键。 目前,我正在尝试添加一个新的播放器,同时使用相应的ID填充PlayerGameRole。但是,我能够执行API,但我的代码不会使用给定的信息填充表。
public Player AddPlayer(Player player){
using (DBContext db = new DBContext())
{
db.Configuration.ProxyCreationEnabled = false;
Player p = new Player();
p.Name = player.Name;
p.Email = player.Email;
p.PlayerGameRole = player.PlayerGameRole;
db.Player.Add(p);
db.SaveChanges();
return p;
}
}
有我的PlayerModel:
public partial class Player{
[DataMember]
public int ID { get; set; }
[DataMember]
[Required]
[StringLength(255)]
public string Name { get; set; }
[DataMember]
[Required]
[StringLength(75)]
public string Email { get; set; }
[DataMember]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PlayerGameRole> PlayerGameRole { get; set; }
}