问题描述
我的项目是Windows应用程序c#,我正在使用实体框架5和.net 4.5。我有房间和床模块,您可以添加,编辑和删除房间和/或床位在我的窗体上我有一个RoomNo和Station的字段,还有添加和删除按钮添加/删除床(s) )到datagridview,然后保存按钮保存房间和床单列表
DB结构
房间(表)
PK_Rooms
FK_Station
房间没有
床(桌子)
PK_Beds
FK_Rooms
BedNo
FullRoomNo(仅限Roomno和Bedno)
RoomStatus
我已经实现了AddRoom 方法将创建新房间和床单列表。
我的问题是我如何实现EditRoom方法,它将检测床上的变化(添加一个新床,和/或编辑床号,和/或删除床)并将其保存到数据库?
更好地了解我的关心请求e看到以前的线程,其中还有AddRoom方法>
编辑:
这里是我的代码到EditRoom方法
M3dEntities m3d = new M3dEntities();
rooms rooms =新房();
床位=新床();
string RoomNo = RoomNoTxt.Text;
int StationID = Int32.Parse(StationCmb.SelectedValue.ToString());
int _SelectedPKRoom = Int32.Parse(SelectedPKRoom);
rooms = m3d.rooms.First(x => x.PK_Rooms == _SelectedPKRoom);
{
rooms.RoomNo = RoomNo;
rooms.FK_Stations = StationID;
}
foreach(BedsDataGridView.Rows中的DataGridViewRow行)
{
床=新床();
beds.Bedno = row.Cells [0] .Value.ToString();
beds.FullRoomNo = row.Cells [1] .Value.ToString();
beds.RoomStatus =可用;
m3d.beds.AddObject(床);
m3d.beds.DeleteObject(床);
}
m3d.SaveChanges();
此代码的问题只是正在更新的空间:(
这是一种方式,我希望它可以帮助你
var room =(from g in m3d.rooms
其中g.RoomNo == RoomNo
select g).FirstOrDefault(); //取一个objecto空间修改
room.FK_Stations = StationID;
foreach(BedsDataGridView.Rows中的DataGridViewRow行)
{
int Bedno = row.Cells [0] .Value.ToString();
var bedsChanged =(from g in m3d.beds
其中g.bedno == Bedno
选择g).FirstOrDefault();
bedsChanged.FullRoomNo = row.Cells [0];
$ / code>
My project is Windows Application c#, I am using Entity Framework 5 and .net 4.5.
I have Rooms and Beds module which you can add, edit and delete Room(s) and/or bed(s)
on my Form I have a field for RoomNo and Station, also Add and Delete button to add/delete bed(s) to datagridview, then Save button to save the Room and list of beds to DB
DB Structure
Rooms (table)
PK_Rooms
FK_Station
RoomNo
Beds (table)
PK_Beds
FK_Rooms
BedNo
FullRoomNo (concat only of Roomno and Bedno)
RoomStatus
I already implemented the "AddRoom" method which will create new Room and list of beds.
my question is how can i implement "EditRoom" method which will detect changes on the beds (added a new bed, AND/OR edited a bed number, AND/OR deleted a bed) and save it to DB?
To have a better understanding to my concern please see previous thread, which also has the AddRoom Method >Entity Framework only saving last row (master details)
Edit:here is my code to EditRoom method
M3dEntities m3d = new M3dEntities();
rooms rooms = new rooms();
beds beds = new beds();
string RoomNo = RoomNoTxt.Text;
int StationID = Int32.Parse(StationCmb.SelectedValue.ToString());
int _SelectedPKRoom = Int32.Parse(SelectedPKRoom);
rooms = m3d.rooms.First(x => x.PK_Rooms == _SelectedPKRoom);
{
rooms.RoomNo = RoomNo;
rooms.FK_Stations = StationID;
}
foreach (DataGridViewRow row in BedsDataGridView.Rows)
{
beds = new beds();
beds.Bedno = row.Cells[0].Value.ToString();
beds.FullRoomNo = row.Cells[1].Value.ToString();
beds.RoomStatus = "Available";
m3d.beds.AddObject(beds);
m3d.beds.DeleteObject(beds);
}
m3d.SaveChanges();
the problem with this code is only room are being updated :(
This is one Way, I hope it helps you
var room= (from g in m3d.rooms
where g.RoomNo == RoomNo
select g).FirstOrDefault(); //Take an objecto room to modify
room.FK_Stations = StationID;
foreach (DataGridViewRow row in BedsDataGridView.Rows)
{
int Bedno = row.Cells[0].Value.ToString();
var bedsChanged= (from g in m3d.beds
where g.bedno== Bedno
select g).FirstOrDefault();
bedsChanged.FullRoomNo = row.Cells[1].Value.ToString();
bedsChanged.RoomStatus = "Available";
}
m3d.SaveChanges();
这篇关于实体框架 - 如何保存多个更改(添加,更新,删除)到DB(主数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!