【问题标题】:Database values not changing数据库值不变
【发布时间】:2017-01-15 11:33:21
【问题描述】:

我的控制器中有一个如下所示的 post 方法,我希望在发布表单时将 AvailableSeats 减少 1。问题是当我第一次运行程序并发布表单时,一切正常,但在随后的时间里,AvailableSeats 的数据库值不会减少 1,它只是保持不变,我无法弄清楚为什么。

[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; }
}

根据要求,这是我的 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();
    }

【问题讨论】:

  • 你跟踪你的行动结果了吗?是否通过if语句?
  • @Valkyriee 它没有通过第一个 if 语句,因为我的 FromLocation 不等于我的 ToLocation 所以如果通过第二个,我看到值发生了变化,但是当它保存到数据库时, AvailableSeats 仍然保持不变。
  • @shA.t 已经尝试过了,但这会使 BusSeats 也减少,这不是我想要的。
  • @AlfMoh 你能试试我的答案吗?如果可行,请告诉我。原因是您没有修改具有相同 id 的当前总线记录。请尝试如果有任何问题让我知道我很乐意提供帮助
  • 我能看到 DBContext 类和 EF 模型类吗?我认为我们在定义关系方面存在问题。请发布它们,我将能够提供帮助。

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


【解决方案1】:

目前,您将公共汽车总座位数减去 1 (24 - 1),每次预订总是返回 23,因为您没有从 Bus 表中更新值 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,
    // ...

【讨论】:

  • 非常感谢。那行得通。而且你的解释很贴切。
【解决方案2】:

您好,我用相同的实体结构尝试了此代码,现在结果是,在每次预订时,公交车表 BusSeats 减少到一个,而在预订表中可用的座位减少到一个。我想这是你想要的。

      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; }
    }

【讨论】:

  • 使用您的代码,您正在减少 Bus Database 表中的 BusSeats 和 Bookings 数据库表中的 AvailableSeats,这不是我想要的。 BusSeats 应该保持不变,而 AvailableSeats 应该总是减少 1。@Abdul 的帖子更好地解释了这一点。无论如何,谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 2013-06-11
  • 2019-11-05
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 2020-08-08
相关资源
最近更新 更多