我在想,如果有人能帮助我在这种情况下:我想救我的数据库更改的,所以我用一个背景下,我有_tblcustomer
这是一个对象,从我的实体类,这里是我的代码:如何使用实体框架将更改保存到数据库?
private void BtnSaveCustomer_Click(object sender, EventArgs e)
{
if (CustomerMode == (int)CustomerModeOperaton.insert)
{
if (!string.IsNullOrWhiteSpace(TxtCustomerName.Text) ||
!string.IsNullOrWhiteSpace(TxtLastName.Text) ||
!string.IsNullOrWhiteSpace(TxtCustomerCode.Text))
{
tblCustomer Customer = new tblCustomer();
Customer.CustomerName = TxtCustomerName.Text.ToString();
Customer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
if (!string.IsNullOrWhiteSpace(TxtCustomerAdress.Text))
{
Customer.CustomerAdresse = TxtCustomerAdress.Text.ToString();
}
else
{
Customer.CustomerAdresse = null;
}
if (!string.IsNullOrWhiteSpace(TxtCustomerPhone.Text))
{
Customer.CustomerPhone = Convert.ToInt32(TxtCustomerPhone.Text);
}
else
{
Customer.CustomerPhone = null;
}
if (!string.IsNullOrWhiteSpace(TxtCustomerCellphone.Text))
{
Customer.CustomerCellPhone = Convert.ToInt32(TxtCustomerCellphone.Text);
}
else
{
Customer.CustomerCellPhone = null;
}
Customer.CustomerLastName = TxtLastName.Text.ToString();
Customer.CustomerID = Guid.NewGuid();
Customer.rowguid = Guid.NewGuid();
using (var Context = new FactorEntities())
{
Context.tblCustomers.Add(Customer);
Context.SaveChanges();
}
MessageBox.Show("اطلاعات مشتری در سیستم ثبت شد");
// status=1;
}
else
{
MessageBox.Show("نام مشتری و نام خانوادگی و کد مشتری باید پر شوند");
}
}
else
{
using (var context = new FactorEntities())
{
var CustomerDetaile = context.tblCustomers.Find(CustomerID);
_tblCustomer = new tblCustomer();
_tblCustomer.CustomerID = CustomerDetaile.CustomerID;
_tblCustomer.CustomerName = TxtCustomerName.Text;
_tblCustomer.CustomerLastName = TxtLastName.Text;
_tblCustomer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
_tblCustomer.CustomerAdresse = TxtCustomerAdress.Text;
context.SaveChanges();
}
MessageBox.Show("اطلاعات در سیستم ثبت شد");
}
}
主要部分是在这里:提前
using (var context =new FactorEntities())
{
var CustomerDetaile = context.tblCustomers.Find(CustomerID);
_tblCustomer = new tblCustomer();
_tblCustomer.CustomerID = CustomerDetaile.CustomerID;
_tblCustomer.CustomerName = TxtCustomerName.Text;
_tblCustomer.CustomerLastName = TxtLastName.Text;
_tblCustomer.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
_tblCustomer.CustomerAdresse = TxtCustomerAdress.Text;
context.SaveChanges();
}
,但我不知道为什么它不保存尚未...
感谢。
'context.SaveChanges();'应该这样做,不是吗? – jdmdevdotnet
@jdmdevdotnet是先生..但它不工作 –
“它不工作”什么不行? 'context.SaveChanges'起作用。什么错误信息? – jdmdevdotnet