0

有谁知道使用循环添加记录的正确方法是什么?

我有一个处理库存的系统,目前我需要能够批量创建库存,因为创建 50-100 个具有不同 ID 的相同物品会很乏味,我所做的是创建一个 MassCreate 视图模型,它基本上需要一个StartID和一个EndID和一个基本库存类,并在控制器中循环通过这两个 ID 之间的差异并创建一条记录

ViewModel 不是问题,并且可以很好地传递数据:

public class MassCreateInventoryViewModel
{
    public Inventory InventoryBase { get; set; }
    public int StartID { get; set; }
    public int EndID { get; set; }
    public IEnumerable<SelectListItem> Products { get; set; }
}

我在某处读到db.SaveChanges()应该在循环之外,因为它应该只被调用一次:

    for (int inventoryID = viewModel.StartID; inventoryID <= viewModel.EndID; inventoryID++)
                {
                    Inventory newInventory = new Inventory
                        {
                            InventoryID = inventoryID,
                            ProductID = viewModel.InventoryBase.ProductID,
                            DateEdited = DateTime.Now,
                            EditedByUserID = WebSecurity.CurrentUserId,
                            CustomProperties = viewModel.InventoryBase.CustomProperties
                        };

                    Database.Inventories.Add(newInventory);

                    if (newInventory.CustomProperties != null && newInventory.CustomProperties.Any())
                    {
                        foreach (CustomDataType dt in newInventory.CustomProperties.Select(x => x.DataType).ToList())
                        {
                            Database.Entry(dt).State = EntityState.Unchanged;
                        }
                    }

                }
                Database.SaveChanges();
        }

但是当我尝试循环时,它会很好地存储第一条记录,然后抛出Collection was modified; enumeration operation may not execute.异常。当我包含Database.SaveChanges()afterAdd方法时,它会引发The property 'InventoryID' is part of the object's key information and cannot be modified.错误。

InventoryIDKey在此表中,但已设置为我可以输入自己的 ID。

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Name = "Inventory ID")]
public new int InventoryID { get; set; }

自定义属性分为两个模型,第一个是基类。

public class CustomProperty
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CustomPropertyID { get; set; }

    public int CustomDataTypeID { get; set; }
    [ForeignKey("CustomDataTypeID")]
    public CustomDataType DataType { get; set; }

    public string PropertyValue { get; set; }
}

第二个是映射到数据库的模型:

[Table("CustomInventoryProperty")]
public  class CustomInventoryProperty : CustomProperty
{
    public int InventoryID { get; set; }
    [ForeignKey("InventoryID")]
    public virtual Inventory Inventory { get; set; }
}
4

1 回答 1

1

用这个替换你的 for 循环:

var dateEdited = DateTime.Now;
for (int inventoryID = viewModel.StartID; inventoryID <= viewModel.EndID; inventoryID++)
{
    Inventory newInventory = new Inventory
    {
        InventoryID = inventoryID, 
        ProductID = viewModel.InventoryBase.ProductID,
        DateEdited = dateEdited,
        EditedByUserID = WebSecurity.CurrentUserId
    };

    if(viewModel.InventoryBase.CustomProperties != null)  
    {
        newInventory.CustomProperties = new List<CustomProperties>(); 
        foreach(var customProperty in viewModel.InventoryBase.CustomProperties)
        {
           newInventory.CustomProperties.Add(customProperty);
        }
    }

    Database.Inventories.Add(newInventory);
    Database.SaveChanges();
}
于 2013-07-16T03:08:17.140 回答