EF数据绑定、编辑、修改、保存记录
EF Databinding, editing, modifying, saving records
我需要一个 EF 示例代码来根据放在搜索字段中的值来填充我的客户表单控件。考虑到此表单上的组合已填满,用户可以创建新记录或通过在文本框上指定值来编辑现有记录。组合中填充了来自辅助 table 的数据库数据(名称),例如国家、州、城市等。
在客户 table 上,我只有这些名称的 ID(外键)。因此,当用户在表单上输入客户 ID 时,如果它不存在,我们将创建一条新记录,否则表单应从数据库中加载整个记录并填写表单上的相应字段,包括显示在组合与记录中的 ID 相匹配的名称,让用户可以选择修改任何字段并将其保存回来。
在非 EF 场景中,我会有类似的东西:
private void txtId_TextChanged(object sender, EventArgs e)
{
sqlStrQuery = "Select FName, LName, Email, Phone, CountryId from Customer where ID= '" + txtId.Text + "'";
SqlCommand sqlCmd = new SqlCommand(sqlStrQuery, sqlConStr);
sqlConStr.Open();
SqlDataReader drCustomer = sqlCmd.ExecuteReader();
if (drCustomer.HasRows)
{
while (drCustomer.Read())
{
txtFirstName.Text = drCustomer.GetValue(1).ToString();
txtlastName.Text = drCustomer.GetValue(2).ToString();
txtEmail.Text = drCustomer.GetValue(3).ToString();
txtPhone.Text = drCustomer.GetValue(4).ToString();
cboCountry.SelectedValue = drCustomer.GetValue(5);
}
}
}
如何将其转换为 EF?
提前致谢。
假设您有一个变量,例如上下文,用于您的数据上下文:
var customer = context.Customers.Find(txtId.Text);
if (customer != null)
{
txtFirstName.Text = customer.FirstName;
txtlastName.Text = customer.LastName;
txtEmail.Text = customer.Email;
txtPhone.Text = customer.Phone;
cboCountry.SelectedValue = customer.CountryId;
}
编辑:搜索多个条件
var matches = context.Customers.AsQueryable();
if (!string.IsNullOrEmpty(txtLastName.Text))
{
matches = matches.Where(m => m.LastName == txtLastName.Text);
}
if (!string.IsNullOrEmpty(txtFirstName.Text))
{
matches = matches.Where(m => m.FirstName == txtFirstName.Text);
}
// repeat for searchable fields
return matches.ToList();
我需要一个 EF 示例代码来根据放在搜索字段中的值来填充我的客户表单控件。考虑到此表单上的组合已填满,用户可以创建新记录或通过在文本框上指定值来编辑现有记录。组合中填充了来自辅助 table 的数据库数据(名称),例如国家、州、城市等。 在客户 table 上,我只有这些名称的 ID(外键)。因此,当用户在表单上输入客户 ID 时,如果它不存在,我们将创建一条新记录,否则表单应从数据库中加载整个记录并填写表单上的相应字段,包括显示在组合与记录中的 ID 相匹配的名称,让用户可以选择修改任何字段并将其保存回来。 在非 EF 场景中,我会有类似的东西:
private void txtId_TextChanged(object sender, EventArgs e)
{
sqlStrQuery = "Select FName, LName, Email, Phone, CountryId from Customer where ID= '" + txtId.Text + "'";
SqlCommand sqlCmd = new SqlCommand(sqlStrQuery, sqlConStr);
sqlConStr.Open();
SqlDataReader drCustomer = sqlCmd.ExecuteReader();
if (drCustomer.HasRows)
{
while (drCustomer.Read())
{
txtFirstName.Text = drCustomer.GetValue(1).ToString();
txtlastName.Text = drCustomer.GetValue(2).ToString();
txtEmail.Text = drCustomer.GetValue(3).ToString();
txtPhone.Text = drCustomer.GetValue(4).ToString();
cboCountry.SelectedValue = drCustomer.GetValue(5);
}
}
}
如何将其转换为 EF? 提前致谢。
假设您有一个变量,例如上下文,用于您的数据上下文:
var customer = context.Customers.Find(txtId.Text);
if (customer != null)
{
txtFirstName.Text = customer.FirstName;
txtlastName.Text = customer.LastName;
txtEmail.Text = customer.Email;
txtPhone.Text = customer.Phone;
cboCountry.SelectedValue = customer.CountryId;
}
编辑:搜索多个条件
var matches = context.Customers.AsQueryable();
if (!string.IsNullOrEmpty(txtLastName.Text))
{
matches = matches.Where(m => m.LastName == txtLastName.Text);
}
if (!string.IsNullOrEmpty(txtFirstName.Text))
{
matches = matches.Where(m => m.FirstName == txtFirstName.Text);
}
// repeat for searchable fields
return matches.ToList();