MVVM 多表新增

Posted weixin_44543308

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVVM 多表新增相关的知识,希望对你有一定的参考价值。

开发工具与关键技术:MVVM
撰写时间:2021年05月25日

在MVVM中多表新增又和WPF的多表新增方法不同,不过都有一个共同的点就是要新增好你所要新增的主表然后成功之后就要获取到这张表要放到那张表中当副键,这样就能同时新增多张表,首先在DAL中写好新增的方法,如下:(如果新增多张表可以在一个方法中写它的新增方法,也可以重新写一个方法,下面就是一个方法中写多个参数)
public void Insert(PW_Order pwOrder,PW_Guest pwGuest,PW_Room pwRoom)

//新增
myModel.PW_Order.Add(pwOrder);
myModel.PW_Guest.Add(pwGuest);
myModel.PW_Room.Add(pwRoom);
if (myModel.SaveChanges() > 0)

MessageBox.Show(“新增成功!”, “系统提示”, MessageBoxButton.OK, MessageBoxImage.Information);

else

MessageBox.Show(“新增失败!”, “系统提示”, MessageBoxButton.OK, MessageBoxImage.Information);


再到页面中的ViewModel中调用ViewModel指定当前窗口方法,如下:
public void InsertOrder(object paremter)

//部门
strDepartment = LoginViewModel.strDepartment;
strOpSate = “新增”;

        //查询订单信息
        var list = (from tbOrder in myModel.PW_Order
                    orderby tbOrder.OrderID descending
                    select new OrderVo
                    
                        OrderID = tbOrder.OrderID,//订单ID
                        OrderNumber = tbOrder.OrderNumber,//订单编号
                    );
        List<OrderVo> lis = new List<OrderVo>(list.ToList());
        OrderVo ordervo = lis[0];
        //实例化窗口
        WD_InsertOrderRoom orderRoom = new WD_InsertOrderRoom(strDepartment, strOpSate);
        //调用ViewModel 指定当前窗口
        var viewModel = (orderRoom.DataContext as InsertGuestOrderViewModel);
        //指定当前操作的实体(实例化)
        viewModel.CurrentOrderEntity = new PW_Order();
        viewModel.CurrentGuestEntity = new PW_Guest();
        viewModel.IsAdd = true;//新增
        viewModel.orderDal = orderRoomDal;//Dal
        orderRoom.ShowDialog();//打开窗口
    

最后到新增页面所对应的ViewModel中写保存方法,如下:
下面这个combox是从页面中传递过来的参数值来获取它的值,
public void SaveOrder(object combox)

if (OrderNo != null)

//控件
ComboBox txt = combox as ComboBox;
blRecommend = true || false;//是否推荐
if (txt.Text.Equals(“一般预订”))

blRecommend = true;

else if (txt.Text.Equals(“特殊预订”))

blRecommend = false;

#region 订单新增
//新增定单信息
PW_Order order = new PW_Order

OrderID = CurrentOrderEntity.OrderID,//订单ID
OrderNumber = OrderNo.ToString(),//订单编号
OperationStatus = strOpSate,//操作状态
OperationID = LoginViewModel.OperationID,//操作ID
ReservationType = blRecommend,//预订类型
OrderStatusID = 1,//默认为预订
ChooseRoomRequest = CurrentOrderEntity.ChooseRoomRequest,//选房要求
MarketID = CurrentOrderEntity.MarketID,//市场ID
PassengerSourceID = PassengerSourceID,//客源ID
ShopDate = CurrentOrderEntity.ShopDate,//到店日期
DepartureDate = CurrentOrderEntity.DepartureDate,//离店日期
LeaveDate = CurrentOrderEntity.LeaveDate,//留房日期
MeaStandard = CurrentOrderEntity.MeaStandard,//餐标
AdultNumber = CurrentOrderEntity.AdultNumber,//成人数
ChildrenNumber = CurrentOrderEntity.ChildrenNumber,//儿童数
Remark = CurrentOrderEntity.Remark,//备注
FromBed = CurrentOrderEntity.FromBed,//免床
HouseCount = CurrentOrderEntity.HouseCount,//房数

            ;
            #endregion
            #region 客人新增
            //添加客人信息
            PW_Guest guest = new PW_Guest
            
                GuestID = CurrentGuestEntity.GuestID,//客人ID
                CustomerTypeID = CurrentGuestEntity.CustomerTypeID,//客人类型ID
                Contacts = CurrentGuestEntity.Contacts,//客人姓名
                Phone = CurrentGuestEntity.Phone,//联系电话
                EnglishName = CurrentGuestEntity.EnglishName,//联系人
            ;
            #endregion
            #region 房间新增
            //房间
            PW_Room room = new PW_Room
            
                RoomLevelID = RoomLevelID,//房间等级ID
            ;
            #endregion
            //查询是否有重复数据
            int count = (from tbOrder in myModel.PW_Order
                         where tbOrder.OrderNumber == strNumber.ToString()
                         select tbOrder).Count();
            #region 判断是否重复
            if (count == 0)
            
                if (IsAdd)
                
                    if (true)
                    
                        //新增
                        orderDal.Insert(order, guest, room);
                        //定单明细
                        SYS_OrderDetails orderDetails = new SYS_OrderDetails
                        
                            OrderID = order.OrderID,//订单ID
                            GuestID = guest.GuestID,//客人ID
                            RoomID = room.RoomID,//房间ID
                        ;
                        orderDal.Inserts(orderDetails);
                       IsWinClose = true; //成功关闭窗体
                     else
                     //新增
                        orderDal.Insert(order, guest, room);
                        //定单明细
                        SYS_OrderDetails orderDetails = new SYS_OrderDetails
                        
                            OrderID = order.OrderID,//订单ID
                            GuestID = guest.GuestID,//客人ID
                            RoomID = room.RoomID,//房间ID
                        ;
                        orderDal.Inserts(orderDetails);


#endregion

结果图:我这里是同时新增四张表的,下面给出的结果图效果是两张表的,如下:




前面两张是新增前的表数据,后两张是新增后的表数据。

以上是关于MVVM 多表新增的主要内容,如果未能解决你的问题,请参考以下文章

MVVM 多表新增

酒店客人登记

酒店管理系统数据库SQl设计思路

酒店管理系统数据库SQl设计思路

2020马尔代夫|可可亚COCOA神秘回归,剧透照,开业打包促销,新增带私人泳池的水屋

客人预定并给予标识