Skip to main content
 首页 » 编程设计

c#之多对多关系基本示例 (MVC3)

2024年02月23日11think

我有一个 MVC3 C# 项目,我有一个 FoodItem 和 FoodItemCategory 模型。两种模型如下图所示:

public class FoodItem 
{ 
    public int ID { get; set; } 
    [Required] 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<FoodItemCategory> Categories { get; set; } 
    public DateTime CreateDate { get; set; } 
} 
 
public class FoodItemCategory { 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<FoodItem> FoodItems { get; set; } 
    public DateTime CreateDate { get; set; } 
}  

我有一个 _CreateOrEdit.cshtml View ,它最初是从 Scaffolder 生成的,我修改它以包含所有类别并选中食品所属的框。一个食品项目可以有许多或所有的类别。 View 如下所示:

@model StackOverFlowIssue.Models.FoodItem 
<div class="editor-label"> 
    @Html.LabelFor(model => model.Name) 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.Name) 
    @Html.ValidationMessageFor(model => model.Name) 
</div> 
<div class="editor-label"> 
    @Html.LabelFor(model => model.Description) 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.Description) 
    @Html.ValidationMessageFor(model => model.Description) 
</div> 
<div class="editor-label"> 
    @Html.LabelFor(model => model.Categories) 
</div> 
<div class="editor-field"> 
    @foreach (var FoodItemCategory in (IEnumerable<StackOverFlowIssue.Models.FoodItemCategory>)ViewBag.Categories){ 
        <input type="checkbox" name="FoodItemCategoryId" value="@FoodItemCategory.ID"  
        @foreach(var c in Model.Categories){ 
            if(c.ID == FoodItemCategory.ID){  
                @String.Format("checked=\"checked\"") 
            }  
        }  
        /> 
        @FoodItemCategory.Name  
        <br /> 
    }  
</div> 
@Html.Hidden("CreateDate", @DateTime.Now) 

如您所见,我有一个嵌套循环,它为每个类别创建一个复选框,在创建每个类别时,我循环遍历并在我的模型的 Categories 属性中检查该特定类别。如果它存在,我设置复选框的 checked 属性。如果您选中一个框并单击保存,在 Controller 上的 HttpPost 操作中,我将执行以下操作:

    [HttpPost] 
    public ActionResult Edit(FoodItem foodItem) 
    { 
        if (ModelState.IsValid) 
        { 
            var cList = Request["CategoryId"].Split(','); 
            List<FoodItemCategory> categories = new List<FoodItemCategory>(); 
 
            foreach (var c in cList) { 
                var ci = Convert.ToInt32(c); 
                FoodItemCategory category = context.FoodItemCategories.Single(x => x.ID == ci); 
                categories.Add(category); 
            } 
 
            context.Entry(foodItem).State = EntityState.Modified; 
            restaurant.Categories = categories; 
            context.SaveChanges(); 
            return RedirectToAction("Index"); 
        } 
        return View(foodItem); 
    } 

我可以一次保存类别。如果我返回 View 并单击保存,我会收到以下错误:

A duplicate value cannot be inserted into a unique index. [ Table name = >FoodItemCategoryFoodItems,Constraint name = PK_FoodItemCategoryFoodItems_00000000000000A8 ] Description: An unhandled exception occurred during the execution of the current web request. Please >review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into >a unique index. [ Table name = FoodItemCategoryFoodItems,Constraint name = >PK_FoodItemCategoryFoodItems_00000000000000A8 ]

Source Error:

Line 97: context.Entry(foodItem).State = EntityState.Modified; Line 98: foodItem.Categories = categories; Line 99: context.SaveChanges(); Line 100: return RedirectToAction("Index"); Line 101: }

不确定这是否重要,但我使用的是 SQLServer Compact Edition 4。我这样做是否正确?像这样的事情的正常编码实践是什么?我知道同样的情况每天都会发生,因为在博客等许多情况下都使用相同的关系模型。

请您参考如下方法:

尝试这样的事情(未经测试):

[HttpPost] 
public ActionResult Edit(FoodItem foodItem) 
{ 
    if (ModelState.IsValid) 
    { 
        int id = foodItem.Id; 
        // Load food item with related categories first 
        var item = context.FoodItems 
                          .Include(f => f.Categories) 
                          .Single(f => f.Id == id); 
 
        // Process changed scalar values 
        context.Entry(item).CurrentValues.SetValues(foodItem); 
 
        // Brute force processing of relations 
        // This can be optimized - instead of deleting all and adding all again 
        // you can manually compare which relations already exists, add new and 
        // remove non existing but let's make that as a homework 
        item.Categories.Clear(); 
 
        var cList = Request["CategoryId"].Split(','); 
 
        foreach (var c in cList)  
        { 
            var ci = Convert.ToInt32(c); 
            // Use find - if category was already loaded in the first query, it will 
            // be reused without additional query to DB 
            var category = context.Categories.Find(ci); 
            // Now add category to attached food item to create new relation 
            item.Categories.Add(category); 
        } 
 
        context.SaveChanges(); 
        return RedirectToAction("Index"); 
    } 
 
    return View(foodItem); 
} 

这看起来效率很低,但因为您处理的是多对多关系,其中可以在 View 中添加或删除关系,所以这是唯一的方法。原因是:

  • 你必须说 EF 哪些关系被添加,哪些被删除
  • 如果您只是简单地添加所有相关类别,您将再次插入关系
  • 您不能说 EF 删除了哪些关系,因为您不传输有关未检查类别的信息

有关分离对象图和处理关系的更多信息是 described here .它与 ObjectContext API 有关,但 DbContext API 只是对其的包装,因此仍然存在相同的限制。