【问题标题】:Add model with link to model object EFModelFirst添加带有模型对象 EFModelFirst 链接的模型
【发布时间】:2011-04-19 15:50:50
【问题描述】:

所以我有 2 个模型:人员和成分,我想要完成的事情是任何人都可以选择一种成分(通过 jQuery 自动完成),输入他们的数据,然后在创建时将其保存到数据库中。

所以应该是这样的:

    public class Person {

            public int PersonID { get; set; }

            [Required(ErrorMessage="Given name is a required field")]
            [DisplayName("Given name")]
            public string GivenName { get; set; }

            [Required(ErrorMessage="Family name is a required field")]
            [DisplayName("Family name")]
            public string FamilyName { get; set; }

            [Required(ErrorMessage = "Birthdate is a required field")]
            [DisplayFormat(DataFormatString = "{0:d}")]
            public DateTime Birthdate { get; set; }

            [DisplayName("Ingredient")]
            public virtual Ingredient Ingredient { get; set; }
    }

还有:

public class Ingredient {
    public int IngredientID { get; set; }

    [Required(ErrorMessage = "Ingredient is a required field")]
    [Remote("IngredientExists", "Person")]
    public string Name { get; set; }

    public virtual ICollection<Person> Persons { get; set; }
}

现在我的视图目前看起来像这样: (sn-p 只是成分部分)

    <div class="editor-label">
    @Html.LabelFor(model => model.Ingredient)
</div>
    <div id="ingredients-list" class="editor-field">        
    @Html.EditorFor(model => model.Ingredient.Name)
        @Html.ValidationMessageFor(model => model.Ingredient.Name)
        @ViewBag.IngredientError
</div>

控制器:

//
// POST: /Person/Create

[HttpPost]
public ActionResult Create(Person person) {
    if (ModelState.IsValid) {
        db.People.Add(person);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    person.Ingredient.Name = "";

    return View(person);
}

现在这一切都有效,除了 1 件事(一个有点大的问题),当我从下拉列表中选择一种成分时,它会保存这个人并在数据库中创建一个新的成分,具有新的 ID 和与那个相同的值选择(即:重复)。 现在,我对 EF 和 LINQ 还很陌生,所以我还没有弄清楚如何去做。任何想法/解决方案将不胜感激!

【问题讨论】:

  • 顺便说一句。类似的问题被问了 100 次,所以请尝试使用搜索。

标签: c# .net asp.net asp.net-mvc-3 entity-framework-4.1


【解决方案1】:

假设名称确实是唯一的,那么您只需选择用户选择的成分:

var ingredient = db.Ingredients.Single(i => i.Name == name); //throws exception if more than one ingredient with that name excists
person.Ingredient = ingredient;
db.Persons.Add(person)

【讨论】:

    【解决方案2】:

    我建议使用外键支持。在您的 person 模型上创建一个 IngredientID 属性,然后将该属性设置为您的 Ingredient 模型中的主键 IngredientID。也许 IngredientID 可以绑定到下拉项的 value 属性

    public class Person {
      public int IngredientID { get; set; }
    }
    

    【讨论】:

    • 我真的不知道如何实现这一点(在我看来,我并没有真正的下拉菜单,只有名称,而不是成分的 ID)。你能详细说明一下吗?
    【解决方案3】:

    如果你这样称呼:

    context.People.Add(person);
    

    您说 EF 那个人是必须插入的新实体,但同时您说所有未附加到上下文的相关实体也是新实体。

    如果你只想添加一个人,你可以试试这个:

    context.People.Attach(person);
    context.Entry(person).State = EntityState.Added;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多