samedi 9 mai 2015

Data storage with communication many -to-many

I got stuck in one place and I can not move on ((( My question is: I have two tables room and reservation . They are connected many to many from table RoomReservation.

public class Room
    {
public int Id { get; set; }
public double CostNight { get; set; }
public virtual ICollection<Reservation> Reservation { get; set; }
}

public class Reservation
    {
public int Id { get; set; }
public string DateOfEntry { get; set; }
public int NumberOfNights { get; set; }
public virtual ICollection<Room> Room { get; set; }
}

On My DbContext I configurated this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Reservation>().HasMany(c => c.Room)
                .WithMany(s => s.Reservation)
                .Map(t => t.MapLeftKey("RoomId")
                    .MapRightKey("ReservationId")
                    .ToTable("RoomReservation"));
        }

After button pressing I save data into database:

public ActionResult Reservation(int number, string date, int roomid)
        {
                var reserv = new Reservation
            {
                NumberOfNights = number,
                DateOfEntry = date
            };
                    db.Reservation.Add(reserv);
                    db.SaveChanges();
}

How can I save data into table RoomReservation?

Aucun commentaire:

Enregistrer un commentaire