数据库值不变

时间:2017-01-15 11:33:22

标签: c# sql-server asp.net-mvc entity-framework linq

我的控制器中有一个post方法,看起来像这样,我想在发布表单时随时减少1。问题是,当我第一次运行程序并发布表单时,一切都运行良好,但随后的时间,AvailableSeats的数据库值不会减少1,它只是保持不变,我可以&#39 ;弄清楚原因。

[HttpPost]
    public ActionResult Create(BookingViewModel viewModel)
    {
        if (viewModel.FromLocationId == viewModel.ToLocationId)
        {
            return RedirectToAction("Index");
        }


        var busFromDb = _context.Buses.First(c=>c.Id == viewModel.BusId);
        var seatsFromDb = busFromDb.BusSeats;

        var reduce = seatsFromDb - 1;

        if (busFromDb != null)
        {
            var book = new Booking
            {
                AvailableSeats = reduce,
                FromLocationId = viewModel.FromLocationId,
                ToLocationId = viewModel.ToLocationId,
                BusId = viewModel.BusId,
                DateTime = viewModel.DateTime,
            };


            _context.Bookings.Add(book);
        }

        _context.SaveChanges();
        return RedirectToAction("Index", "Home");
    }

我的预订课程

public class Booking
{
    [Required]
    public int Id { get; set; }

    public FromLocation FromLocation { get; set; }
    public int FromLocationId { get; set; }

    public ToLocation ToLocation { get; set; }
    public int ToLocationId { get; set; }

    public Bus Bus { get; set; }

    [Required]
    public int BusId { get; set; }
    public DateTime DateTime { get; set; }

    public int AvailableSeats { get; set; }
}

我的公共汽车课程

public class Bus
{
    public int Id { get; set; }
    public string BusNumber { get; set; }
    public BusService BusService { get; set; }
    public int BusSeats { get; set; }
}

BookingsDb

BusesDb

根据要求,这是我的DbContext类

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Bus> Buses { get; set; }
    public DbSet<Booking> Bookings { get; set; }
    public DbSet<FromLocation> FromLocations { get; set; }
    public DbSet<ToLocation> ToLocations { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

之后我在控制器中初始化它。

public class BookingsController : Controller
{
    private ApplicationDbContext _context;

    public BookingsController()
    {
        _context = new ApplicationDbContext();
    }

2 个答案:

答案 0 :(得分:2)

目前,您要扣除总公共汽车座位数1 (24 - 1),每次预订总共返回23,因为您没有从公交车表更新值24,我假设其总数是在一辆公共汽车上的座位。相反,您可以计算每辆公交车的总预订量,以便为您提供剩余座位。

int seatsFromDb = busFromDb.BusSeats; //24
int soldSeats = _context.Bookings.Count(b => b.BusId == viewModel.BusId); //2 (you need to add more filters)

int remainingSeats = seatsFromDb - (soldSeats + 1) //include the current booking (sold 3)

var book = new Booking
{
    AvailableSeats = remainingSeats,
    // ...

答案 1 :(得分:0)

您好我尝试了具有相同权利结构的此代码,现在结果是每次预订时总线表BusSeats减少到一个并且在Booking表中可用的座位减少到一个。我想这是你想要的。

      public ActionResult Index()
      {
        -- I hardcoded id because I donthave all view model but with this i was able to achieve the result you required.
        int BusId = 1;
        var _context = new TestContext();
        var busFromDb = _context.Buses.FirstOrDefault(c => c.Id == BusId);
        var seatsFromDb = busFromDb.BusSeats;

        var reduce = seatsFromDb - 1;

        if (busFromDb != null)
        {
            busFromDb.BusSeats = busFromDb.BusSeats - 1;
            var book = new Booking
            {
                AvailableSeats = reduce,
                BusId = BusId,
                DateTime =DateTime.Now,
                Bus = busFromDb
            };
            book.Bus = busFromDb;
            _context.Entry(busFromDb).State = EntityState.Modified;


            _context.Bookings.Add(book);
        }

        _context.SaveChanges();
        return View();
    }

以下是我的EF课程

       public class TestContext:DbContext
        {
            public TestContext()
                : base("name=TestConnection")
            {
            }
          public virtual  DbSet<Bus> Buses { get; set; }
            public virtual DbSet<Booking> Bookings { get; set; }
        }

    }
    public class Booking
    {    
        public int Id { get; set; }
        public Bus Bus { get; set; }  
        public int BusId { get; set; }
        public DateTime DateTime { get; set; }
        public int AvailableSeats { get; set; }   
    }

    public class Bus
    {
        public int Id { get; set; }
        public string BusNumber { get; set; }
        public int BusSeats { get; set; }
    }