0

なぜ以下は戦略とセグメントの重複エントリを生成するのですか?任意のガイダンスをいただければ幸いです。

public ActionResult Create(Program program)
{
    db.Programs.Add(program);

    Strategy strategy = db.Strategies.Where(s => s.Name == program.Strategy.Name).SingleOrDefault();
    Segment segment = db.Segments.Where(m => m.Name == program.Segment.Name).SingleOrDefault();

    // Make sure not to save duplicate entries of segment & strategy
    if (strategy != null)
    {
        program.Strategy = strategy;
        db.Entry(strategy).State = EntityState.Unchanged;
    }

    if (segment != null)
    {
        program.Segment = segment;
        db.Entry(segment).State = EntityState.Unchanged;
    }

    db.SaveChanges();
}
4

1 に答える 1

0

試す

 db.Entry(strategy).State = EntityState.Unchanged; 

 db.Entry(segment).State = EntityState.Unchanged; 
于 2012-08-29T08:14:36.380 に答える