2

I've got an MVC5 web application that uses SQL Server 2008 as a back end database along with the Entity Framework 6. There's no error in adding code but data can not get stored in to database.

my model context.cs looks like

public partial class checkin_checkoutEntities2 : DbContext
    {
        public checkin_checkoutEntities2()
            : base("name=checkin_checkoutEntities2")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<Advance_Booking> Advance_Bookings { get; set; }
        public virtual DbSet<employee_reg> employee_reg { get; set; }
        public virtual DbSet<Extend_Stay> Extend_Stay { get; set; }
        public virtual DbSet<Guest_reg> Guest_reg { get; set; }
        public virtual DbSet<RoomBooking> RoomBooking { get; set; }
        public virtual DbSet<Rooms_ms> Rooms_ms { get; set; }
    } 

and my adding method is as below

 ab.Room_id = Convert.ToInt32(ab_rm_no.room_id);
 ab.Guest_id = Convert.ToInt32(frm["guest_id"].ToString());
 ab.Expected_checkin  = Convert.ToDateTime(frm["BookedDateFR"].ToString());
 ab.Expected_checkout = Convert.ToDateTime(frm["BookedDateTO"].ToString());
 
 db.Advance_Bookings.Add(ab);
 db.SaveChanges();
9
  • did you try to to debug and see what's getting added in db.Advance_Bookings.Add(ab);? Jul 6, 2017 at 6:47
  • Does your DataContext load entities from the database? Your connection string seems suspicious, to say the least. Jul 6, 2017 at 6:52
  • @RomanoZumbé Yes it loads entities form db. Jul 6, 2017 at 7:21
  • @MalavSevak And your user can insert into the desired tables? Perhaps confirm that from Management Studio Jul 6, 2017 at 7:23
  • @KushalR I want to add entry into database table . and there's no error for sure. i have tried debugging by putting breakpoints Jul 6, 2017 at 7:23

2 Answers 2

2

According to the comments, the db.SaveChanges(); was wrapped in a transaction

using (var transaction = new System.Transactions.TransactionScope())
{
    // some changes and db.SaveChanges();
}

and it was working only without the transaction.

The reason is, that a transaction needs to be committed in order to persist the saved changes.

using (var transaction = new System.Transactions.TransactionScope())
{
    // some changes and db.SaveChanges();

    transaction.Commit();
}

Otherwise the changes would be discarded at the end of the transaction block.

1

Can you change your code a bit and try following: Rather using db.Advance_Bookings.Add(ab) can you use following:

db.Entry(ab).State = EntityState.Added;

Also, as far my understanding this problem can only occur if the DbContext is disconnected (as it's not giving any error). You can try to wrap your code in a using block

using (var ab = new DbContext()) 
{
//instantiate the object and saveChanges.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.