我在想,如果有人能幫助我在這種情況下:我想救我的數據庫更改的,所以我用一個背景下,我有_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