C# EF6 API以多对多关系更新数据
我正在做一个有卡片、特效和系列的API。一张牌可以有很多系列和很多效果,系列可以有很多张牌,效果可以有很多张牌。我修复了循环引用,现在我的问题是,当我想添加/更新(使用带有POST/PUT的Postman)例如一张带有现有系列的新卡时,它会创建一个新系列,我如何修复C# EF6 API以多对多关系更新数据,c#,entity-framework,asp.net-web-api,C#,Entity Framework,Asp.net Web Api,我正在做一个有卡片、特效和系列的API。一张牌可以有很多系列和很多效果,系列可以有很多张牌,效果可以有很多张牌。我修复了循环引用,现在我的问题是,当我想添加/更新(使用带有POST/PUT的Postman)例如一张带有现有系列的新卡时,它会创建一个新系列,我如何修复 public class Card { public Card() { this.Effects = new HashSet<Effect>(); this.Series
public class Card
{
public Card()
{
this.Effects = new HashSet<Effect>();
this.Series = new HashSet<Serie>();
}
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Number { get; set; }
public string Text { get; set; }
[Required]
public int Cost { get; set; }
[Required]
public string Element { get; set; }
[Required]
public string Job { get; set; }
[Required]
public virtual ICollection<Serie> Series { get; set; }
public int? Strengh { get; set; }
public string Picture { get; set; }
public virtual ICollection<Effect> Effects { get; set; }
}
public class Serie
{
public Serie()
{
this.Cards = new HashSet<Card>();
}
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Card> Cards { get; set; }
}
但现在,它什么也不更新,它会像我想要的那样返回卡,但不要在db中更新它,知道吗?哪个EF版本?我使用的是EF6:o
// PUT: api/Cards/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutCard(int id, Card card)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != card.Id)
{
return BadRequest();
}
db.Entry(card).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CardExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
db.Entry(card).State = EntityState.Modified;
foreach (var item in card.Effects)
{
db.Entry(item).State = EntityState.Modified;
}
foreach (var item in card.Series)
{
db.Entry(item).State = EntityState.Modified;
}
db.SaveChanges();