在MVC2中使用EF更新表

时间:2011-07-20 15:28:22

标签: sql linq-to-sql stored-procedures entity-framework-4 linq-to-entities

我写了一个类似的SQL查询:

UPDATE Product
SET    QtyLeft = Product.QtyLeft - BookingItem.Qty
FROM   BookingItem 
JOIN   Product 
  ON   BookingItem.ProductID = Product.ID
WHERE  (BookingItem.BookingID = @bookingID)

我想要做的是,在预订时运行此查询.StatusId为True所以它应该是这样的:

if(booking.StatusId == True) { //run the Query} else { do something else}

我正在使用的技术: MVC 2 EF 4

编辑:我做过类似的事情:

using (ApextrackdaysEntities entities = new ApextrackdaysEntities())
{
  IBookingItems bookingItemResp = new BookingItemRepository();
  IQueryable<BookingItem> bookingItem = bookingItemResp.GetAllBookingItem(booking.ID);
  foreach (BookingItem items in bookingItem)
  {
       int productID = items.ProductID;
       IProductRepository prodResp = new ProductRepository();
       Product products = prodResp.GetProductsByID(productID);
       products.QtyLeft = products.QtyLeft - items.Qty;
       prodResp.Save();
  }
}

我想知道,我如何使用EF实现这一点,应该为它创建一个存储过程,或者Linq To Sql,任何建议或替代方案都将受到高度赞赏。

0 个答案:

没有答案