DbContext.SaveChanges()不会将更改保存到数据库,但会更新网格。我想要的方法不是bSave_Click
方法。如何强制它更新数据库?
Windows窗体应用程序的代码如下:
namespace WinFormsApp
{
public partial class MainForm : Form
{
private List<User> users;
private List<UserType> userTypes;
private List<DataGridViewUserDTO> usersForGrid;
public MainForm()
{
InitializeComponent();
GetData();
PrepareGrid();
FillGrid();
}
public void GetData()
{
using (var dbContext = new DataInFileEntities())
{
userTypes = dbContext.UserTypes.ToList();
users = dbContext.Users.ToList();
usersForGrid = users.Select(x => new DataGridViewUserDTO() { Id = x.Id, FirstName = x.FirstName, LastName = x.LastName, UserTypeId = x.UserTypeId, CreatedById = x.CreatedById, Created = x.Created, EditedById = x.EditedById, Edited = x.Edited }).ToList();
}
}
public void FillGrid()
{
var gridBindingList = new BindingList<DataGridViewUserDTO>(usersForGrid);
gridBindingSource = new BindingSource(gridBindingList, null);
dgvUsers.DataSource = gridBindingSource;
}
public void PrepareGrid()
{
dgvUsers.AutoGenerateColumns = false;
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Id";
col.HeaderText = "Id";
col.Name = "Id";
col.ReadOnly = true;
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
dgvUsers.Columns.Add(col);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "FirstName";
col.HeaderText = "First Name";
col.Name = "FirstName";
dgvUsers.Columns.Add(col);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "LastName";
col.HeaderText = "Last Name";
col.Name = "LastName";
dgvUsers.Columns.Add(col);
DataGridViewComboBoxColumn userTypeCol = new DataGridViewComboBoxColumn();
userTypeCol.DataPropertyName = "UserTypeId";
userTypeCol.HeaderText = "User Type";
userTypeCol.Name = "UserTypeId";
userTypeCol.DataSource = new BindingList<UserType>(userTypes);
userTypeCol.DisplayMember = "Name";
userTypeCol.ValueMember = "Id";
dgvUsers.Columns.Add(userTypeCol);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Edited";
col.HeaderText = "Edited";
col.Name = "Edited";
col.ReadOnly = true;
dgvUsers.Columns.Add(col);
DataGridViewComboBoxColumn editedByCol = new DataGridViewComboBoxColumn();
editedByCol.DataPropertyName = "EditedById";
editedByCol.HeaderText = "Edited By";
editedByCol.Name = "EditedById";
editedByCol.DataSource = new BindingList<DataGridViewUserDTO>(usersForGrid);
editedByCol.DisplayMember = "Name";
editedByCol.ValueMember = "Id";
editedByCol.ReadOnly = true;
dgvUsers.Columns.Add(editedByCol);
col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "Created";
col.HeaderText = "Created";
col.Name = "Created";
col.ReadOnly = true;
dgvUsers.Columns.Add(col);
DataGridViewComboBoxColumn createdByCol = new DataGridViewComboBoxColumn();
createdByCol.DataPropertyName = "CreatedById";
createdByCol.HeaderText = "Created By";
createdByCol.Name = "CreatedById";
createdByCol.DataSource = new BindingList<DataGridViewUserDTO>(usersForGrid);
createdByCol.DisplayMember = "Name";
createdByCol.ValueMember = "Id";
createdByCol.ReadOnly = true;
dgvUsers.Columns.Add(createdByCol);
}
private void bSave_Click(object sender, EventArgs e)
{
using (var dbContext = new DataInFileEntities())
{
foreach (var row in usersForGrid)
{
var dbItem = dbContext.Users.Where(x => x.Id == row.Id).SingleOrDefault();
if (dbItem != null)
{
if (dbItem.FirstName != row.FirstName || dbItem.LastName != row.LastName || dbItem.UserTypeId != row.UserTypeId)
{
dbItem.FirstName = row.FirstName;
dbItem.LastName = row.LastName;
dbItem.UserTypeId = row.UserTypeId;
dbItem.Edited = DateTime.UtcNow;
dbItem.EditedById = 1;
}
}
}
dbContext.SaveChanges();
}
GetData();
FillGrid();
}
}
public class DataGridViewUserDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Name { get { return FirstName + " " + LastName; }}
public byte UserTypeId { get; set; }
public Nullable<System.DateTimeOffset> Edited { get; set; }
public Nullable<int> EditedById { get; set; }
public System.DateTimeOffset Created { get; set; }
public int CreatedById { get; set; }
}
}