【发布时间】:2014-06-12 08:33:17
【问题描述】:
我有一个使用 EF6.1 的 WinForms 应用程序。我在表单上自动填充了 bindingsource 和 bindingnavigator。在 GridView 中双击一个客户会引导我进入一个新表单,在该表单中我会将此客户的详细信息显示为详细信息,并可选择添加新客户。 bindingsource 只有一项,context.Local 也一样。单击导航器中的加号会添加另一个客户,并且 bindingsource 现在有两个项目要跟踪,而 context.Local 仍然只有一个。我需要让我的上下文知道这条新记录,但是我尝试的所有操作似乎都向绑定源添加了另一条记录。尝试保存时,这会给我一个例外,因为不填充不可为空的列。代码是这样的:
private void frmCustomer_Load(object sender, EventArgs e)
{
using (context = new CarsEntities(Properties.Settings.Default.connectionString))
{
customer = context.Customers.Find(Id);
customerBindingSource.DataSource = customer;
}
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
context.Customers.Local.Add((Customer)customerBindingSource.Current);
}
private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
foreach (var cust in context.Customers.Local.ToList())
{
if (cust.CustomerNo == null)
{
cust.CustomerNo = "8";//just for testing, will use a value from a sequence or something...
}
}
customerBindingSource.EndEdit();
context.SaveChanges();
}
}
【问题讨论】:
标签: c# entity-framework data-binding