/// <summary>
/// 航班
/// </summary>
public class FlightBooking
{
/// <summary>
/// 航班Id
/// </summary>
public int FlightId { get; set; }
/// <summary>
/// 航班名称
/// </summary>
public string FilghtName { get; set; }
/// <summary>
/// 航班号
/// </summary>
public string Number { get; set; }
/// <summary>
/// 出行日期
/// </summary>
public DateTime TravellingDate { get; set; }
}
/// <summary>
/// 预订
/// </summary>
public class Reservation
{
/// <summary>
/// 预订Id
/// </summary>
public int BookingId { get; set; }
/// <summary>
/// 预订人
/// </summary>
public string Name { get; set; }
/// <summary>
/// 预订日期
/// </summary>
public DateTime BookingDate { get; set; } = DateTime.Now;
}
public class TripReservation
{
public FlightBooking Filght { get; set; }
public Reservation Hotel { get; set; }
}
public class HotelFlightConfiguration : DbConfiguration
{
public HotelFlightConfiguration()
{
SetDatabaseInitializer(new DropCreateDatabaseIfModelChanges<HotelDBContext>());
SetDatabaseInitializer(new DropCreateDatabaseIfModelChanges<FlightDBContext>());
}
}
[DbConfigurationType(typeof(HotelFlightConfiguration))]
public class FlightDBContext : DbContext
{
public FlightDBContext() : base("name=flightConnection")
{ }
public DbSet<FlightBooking> FlightBookings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new FlightBookingMap());
base.OnModelCreating(modelBuilder);
}
}
[DbConfigurationType(typeof(HotelFlightConfiguration))]
public class HotelDBContext: DbContext
{
public HotelDBContext():base("name=reservationConnction")
{ }
public DbSet<Reservation> Reservations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ReservationMap());
base.OnModelCreating(modelBuilder);
}
}
public class FlightBookingMap : EntityTypeConfiguration<FlightBooking>
{
public FlightBookingMap()
{
//table
ToTable("FlightBookings");
//key
HasKey(k => k.FlightId);
//property
Property(p => p.FilghtName).HasMaxLength(50);
Property(p => p.Number);
Property(p => p.TravellingDate);
}
}
public class ReservationMap : EntityTypeConfiguration<Reservation>
{
public ReservationMap()
{
//table
ToTable("Reservations");
//key
HasKey(k => k.BookingId);
//property
Property(p => p.BookingId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Property(p => p.Name).HasMaxLength(20);
Property(p => p.BookingDate);
}
}
<connectionStrings> <add name="reservationConnction" connectionString="Data Source=WANGPENG;Initial Catalog=ReservationDb;Integrated Security=true" providerName="System.Data.SqlClient" /> <add name="flightConnection" connectionString="Data Source=WANGPENG;Initial Catalog=FlightDb;Integrated Security=true" providerName="System.Data.SqlClient" /> </connectionStrings>
public class MakeReservation
{
FlightDBContext flight;
HotelDBContext hotel;
public MakeReservation()
{
flight = new FlightDBContext();
hotel = new HotelDBContext();
}
//处理事务方法
public bool ReservTrip(TripReservation trip)
{
bool reserved = false;
//绑定处理事务范围
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
try
{
//航班信息
flight.FlightBookings.Add(trip.Filght);
flight.SaveChanges();
//预约信息
hotel.Reservations.Add(trip.Hotel);
hotel.SaveChanges();
reserved = true;
//完成事务并提交
scope.Complete();
}
catch (Exception ex)
{
throw ex;
}
}
return reserved;
}
}
public class TripController : Controller
{
MakeReservation reserv;
public TripController()
{
reserv = new MakeReservation();
}
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
return View(new TripReservation());
}
[HttpPost]
public ActionResult Create(TripReservation tripinfo)
{
try
{
tripinfo.Filght.TravellingDate = DateTime.Now;
tripinfo.Hotel.BookingDate = DateTime.Now;
var res = reserv.ReservTrip(tripinfo);
if (!res)
{
return View("Error");
}
}
catch (Exception)
{
return View("Error");
}
return View("Success");
}
}
@model EntityFrameworkTransactionScope.Data.Entity.TripReservation
@{
ViewBag.Title = "Create";
}
<h2 class="text-center">旅游出行</h2>
@using(Html.BeginForm()){
<table class="table table-condensed table-striped table-bordered">
<tr>
<td>
<table class="table table-condensed table-striped table-bordered">
<tr>
<td colspan="2" class="text-center">
航班信息
</td>
</tr>
<tr>
<td>
航班Id:
</td>
<td>
@Html.EditorFor(m => m.Filght.FlightId)
</td>
</tr>
<tr>
<td>
航班名称:
</td>
<td>
@Html.EditorFor(m => m.Filght.FilghtName)
</td>
</tr>
<tr>
<td>
航班号:
</td>
<td>
@Html.EditorFor(m => m.Filght.Number)
</td>
</tr>
</table>
</td>
<td>
<table class="table table-condensed table-striped table-bordered">
<tr>
<td colspan="2" class="text-center">
预约信息
</td>
</tr>
<tr>
<td>
预约Id:
</td>
<td>
@Html.EditorFor(m => m.Hotel.BookingId)
</td>
</tr>
<tr>
<td>
客户名称
</td>
<td>
@Html.EditorFor(m => m.Hotel.Name)
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2" class="text-center">
<input type="submit" value="提交预约" />
</td>
</tr>
</table>
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有