双重输入

时间:2012-08-29 08:09:40

标签: asp.net entity-framework

为什么以下内容会为策略&分割?任何指导都将不胜感激。

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();
}

1 个答案:

答案 0 :(得分:0)

尝试

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

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