实体框架只保存最后一行(主数据) [英] Entity Framework only saving last row (master details)
问题描述
我有房间和床模块,您可以添加,编辑和删除房间和/或床位在我的窗体上我有一个RoomNo和Station的字段,还有添加和删除按钮添加/删除床(s) )to datagridview
DB结构
房间(表)
PK_Rooms
FK_Station
RoomNo
床(桌)
PK_Beds
FK_Rooms
BedNo
FullRoomNo(仅限Roomno和Bedno)
RoomStatus
下面是我的添加方法来添加房间和床到DB
M3dEntities m3d = new M3dEntities();
rooms rooms =新房();
床位=新床();
string RoomNo = RoomNoTxt.Text;
int StationID = Int32.Parse(StationCmb.SelectedValue.ToString());
var roomnoexist = from g in m3d.rooms
其中g.RoomNo == RoomNo
select g;
if(roomnoexist.Any())
{
MessageBox.Show(Room No. Already Exist!);
}
else
{
rooms.RoomNo = RoomNo;
rooms.FK_Stations = StationID;
m3d.rooms.AddObject(rooms);
}
foreach(BedsDataGridView.Rows中的DataGridViewRow行)
{
beds.Bedno = row.Cells [0] .Value.ToString();
beds.FullRoomNo = row.Cells [1] .Value.ToString();
beds.RoomStatus =可用;
m3d.beds.AddObject(床);
}
m3d.SaveChanges();
(例如:添加房间1201,带4张床)
只剩下最后一张床到DB。
我的问题:
- 为什么只保存最后一行?
- 如何将所有行保存到床上?
- 如何使用编辑方法(在当前房间添加另一张床或删除床)?
您应该移动床位=新床();
在 foreach
中的说明如下:
code> foreach(BedsDataGridView.Rows中的DataGridViewRow行)
{
床位=新床(); //这是它的地方。
beds.Bedno = row.Cells [0] .Value.ToString();
beds.FullRoomNo = row.Cells [1] .Value.ToString();
beds.RoomStatus =可用;
m3d.beds.AddObject(床);
}
Wihtout你总是在最后一个
其中最后一行包含最后一行,并只保存该行。
旁注:以获得某些性能如果您正在插入多个对象,请使用 AddRange
var bedsCollection = new List< beds>();
foreach(BedsDataGridView.Rows中的DataGridViewRow行)
{
床位=新床(); //这是它的地方。
beds.Bedno = row.Cells [0] .Value.ToString();
beds.FullRoomNo = row.Cells [1] .Value.ToString();
beds.RoomStatus =可用;
bedsCollection.Add(beds); //更新集合而不是DbSet
}
m3d.beds.AddRange(bedsCollection);
m3d.SaveChanges();
My project is c# Windows Application, 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
DB Structure
Rooms (table)
PK_Rooms
FK_Station
RoomNo
Beds (table)
PK_Beds
FK_Rooms
BedNo
FullRoomNo (concat only of Roomno and Bedno)
RoomStatus
below is my Add Method to add the room and beds to DB
M3dEntities m3d = new M3dEntities();
rooms rooms = new rooms();
beds beds = new beds();
string RoomNo = RoomNoTxt.Text;
int StationID = Int32.Parse(StationCmb.SelectedValue.ToString());
var roomnoexist = from g in m3d.rooms
where g.RoomNo == RoomNo
select g;
if (roomnoexist.Any())
{
MessageBox.Show("Room No. Already Exist!");
}
else
{
rooms.RoomNo = RoomNo;
rooms.FK_Stations = StationID;
m3d.rooms.AddObject(rooms);
}
foreach (DataGridViewRow row in BedsDataGridView.Rows)
{
beds.Bedno = row.Cells[0].Value.ToString();
beds.FullRoomNo = row.Cells[1].Value.ToString();
beds.RoomStatus = "Available";
m3d.beds.AddObject(beds);
}
m3d.SaveChanges();
(Ex: add room 1201 with 4 beds) Only last row of bed is saved to DB.
My Questions:
- Why only save the last row?
- How to save all rows to beds?
- How can i do it for Edit Method (To add another bed or delete bed on the current room)?
You should move the beds beds = new beds();
instruction inside your foreach
statement like this:
foreach (DataGridViewRow row in BedsDataGridView.Rows)
{
beds beds = new beds(); // Here is its place.
beds.Bedno = row.Cells[0].Value.ToString();
beds.FullRoomNo = row.Cells[1].Value.ToString();
beds.RoomStatus = "Available";
m3d.beds.AddObject(beds);
}
Wihtout that you are always oiverriden the last instance of beds
which end up containing your last row and save only that row.
Side note: to gain some performance use AddRange
if you are inserting multiple objects once like this:
var bedsCollection = new List<beds>();
foreach (DataGridViewRow row in BedsDataGridView.Rows)
{
beds beds = new beds(); // Here is its place.
beds.Bedno = row.Cells[0].Value.ToString();
beds.FullRoomNo = row.Cells[1].Value.ToString();
beds.RoomStatus = "Available";
bedsCollection.Add(beds); // update the collection not the DbSet
}
m3d.beds.AddRange(bedsCollection);
m3d.SaveChanges();
这篇关于实体框架只保存最后一行(主数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!