Asp.net/entity - 从数据库暂时“删除”项目?
您好,
我正在研究在我正在开发的ASP.NET MVC项目中构建购物车应用程序。这是一个电子商务网站,每个项目都是独一无二的 - 我只有每个产品中的一个。因此,当用户将商品添加到购物车(但尚未付款)时,我希望购物车中的商品不再可供任何其他(或甚至相同)用户添加到购物车中。
这些项目存储在数据库中,并在用户访问相应页面时动态加载。我正在使用实体。据我所知,在使用Entity时,更改不会自动提交到数据库 - 您必须调用SaveChanges方法。考虑到这一点,如果我使用DBContext(或任何我称之为)类删除它,它是否工作(即,添加到购物车中的项目是否会被加载),但实际上并没有调用SaveChanges ()直到退房流程完成后?
此外,Sessions是实施购物车的合适方式吗?
如果这个问题不清楚或有明显的答案,我很抱歉 - 我是ASP.NET的新手!
什么我试过了:
老实说,还不多。我仍然主要处于计划阶段。
Hello,
I am looking at building a shopping cart application in an ASP.NET MVC project I am working on. It is an e-commerce site in which each item is unique - I only have one of every product. As such, when a user adds an item to cart (but does not yet pay), I would like it so that the item in the cart is no longer available for any other (or even the same) user to add to their cart.
The items are stored in a database and dynamically loaded when the user visits the appropriate page. I am using Entity. I understand that, when using Entity, changes are not automatically committed to the database - you must call the SaveChanges method. With this in mind, would it 'work' (that is, would the item that was added to cart not be loaded) if I 'deleted' it using the DBContext (or whatever I call it) class, but do not actually call SaveChanges() until after the check-out process is complete?
Also, are Sessions an appropriate way to implement a shopping cart?
I am sorry if this question is unclear or one with an obvious answer - I am new to ASP.NET!
What I have tried:
Honestly, not much yet. I am still mostly in the planning phase.
在您的产品上有一个数量字段,您在创建产品时将其设置为1。当有人将它添加到他们的篮子中时将数量减少1,并且只允许人们将数量添加到他们的篮子中,如果数量> 0。
你的问题当有人在他们的购物篮中添加商品但不购买时,只需关闭浏览器或在其他地方导航。为了预先处理您的下一个问题,当有人关闭浏览器或导航时,您无法判断,并且您无法阻止他们关闭浏览器或导航。您需要某种形式的自动化任务来识别废弃的篮子,即一小时内未更新的篮子,并从篮子中取出物品并将其数量重新设置为1。 />
互联网不是销售您只有一件物品的好地方。如果eBay和销售汽车等的每个网站都无法解决这个问题,那么你也不太可能。
Have a "Quantity" field on your product which you set to 1 when you create the product. When someone adds it to their basket reduce the quantity by 1, and only allow people to add the item to their basket if the quantity is >0.
Your problems start when someone adds an item to their basket but doesn't buy and simply closes their browser or navigates somewhere else. To pre-empt your next questions no you can't tell when someone closes the browser, or navigates away, and you can't stop them closing the browser or navigating away. You'll need some form of automated tasks that identifies "abandoned baskets", ie baskets that haven't been updated in, say, an hour, and remove the items from the basket and re-set their quantity back to 1.
The internet is not a good place to sell items you only have one of. If eBay and every website that sells cars etc can't solve this problem it is unlikely you will either.