好吧,我有一个奇怪的问题。我有订单,订单有购物车,购物车有购物车物品集合,其中包括产品及其数量:
命令:
public class Order : DatabaseEntity
{
public Order(Cart cart)
{
Cart = cart;
}
public int Id { get; set; }
[Required]
public Cart Cart { get; set; }
...
}
大车:
public class Cart : DatabaseEntity
{
public ICollection<CartItem> Items { get; set; }
public void AddItem(Product product)
{
// Check is there such item, is Items null, update quantity
// if there is already such item bla bla
Items.Add(new CartItem(product));
}
...
}
购物车项目:
public class CartItem : DatabaseEntity
{
public CartItem(Product product)
{
Product = product;
Quantity = 1;
}
public Product Product { get; set; }
public int Quantity { get; private set; }
}
问题是当我创建新订单并尝试下订单时,我在数据库中得到了重复的产品记录。为什么会这样,我从来没有遇到过这个问题。缺少什么?oO:
[HttpPost]
public ActionResult MakeOrder(MakeOrderViewModel makeOrderModel)
{
...
var cart = Session["cart"] as Cart;
var order = new Order(cart);
orderRepository.PlaceOrder(order);
...
}
订单仓库:
public void PlaceOrder(Order order)
{
_repository.InsertOrUpdate(order);
}
和存储库本身:
public class EFDatabaseRepository
{
...
public void InsertOrUpdate<TObject>(TObject entity) where TObject : DatabaseEntity
{
var entry = _database.Entry(entity);
if(entry.State == EntityState.Detached)
{
// New entity
_database.Set<TObject>().Add(entity);
}
else
{
// Existing entity
_database.Entry(entity).State = EntityState.Modified;
}
Save();
}
private void Save()
{
_database.SaveChanges();
}
}
DatabseEntity 只是带有 id 字段的类
public class DatabaseEntity
{
public int Id { get; set; }
}
我的数据库上下文类:
public class DataBase : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<CartItem> CartItems { get; set; }
public DbSet<Cart> Carts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CartItem>().HasRequired(ci => ci.Product);
modelBuilder.Entity<Cart>().HasMany(c => c.Items);
base.OnModelCreating(modelBuilder);
}
}