[英]How save my changes to database with Entity Framework?
我想知道在这种情况下是否有人可以帮助我:我试图将所做的更改保存到数据库中,所以我使用了上下文,并且我有_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();
}
但我不知道为什么它还不能保存...
提前致谢。
我看不到您在上下文中添加_tblCustomer对象的任何地方,例如在“主要部分”中
context.tblCustomers.Add(_tblCustomer);
相反,如果您要修改现有对象,则应编写
CustomerDetaile.CustomerId = "the new id"
现在它将被保存。
您现在正在做的是创建一个新客户,为其分配值,而不对其进行任何处理。
在这种情况下,要将新对象保存到数据库,您将需要使用:
var obj = context.TableName.New();
obj.Name = "BLA";
obj.Salary = 32;
context.TableName.Add(obj);
context.SaveChanges();
要编辑表中的现有对象:
var obj = context.TableName.Find(id);
obj.Name = "BLA";
obj.Salary = 32;
context.Entry(obj).State = EntryState.Modified;
context.SaveChanges();
它始终对我有用=)将这个概念应用于您的代码,它可能会起作用。
我不应该做一个新的...我必须为自己的选择编写代码...我通过@ChristianKouamé得到了它,这是我的代码:
using (var context =new FactorEntities())
{
var CustomerDetaile= context.tblCustomers.Find(CustomerID);
CustomerDetaile.CustomerID = CustomerID;
if (!string.IsNullOrWhiteSpace(TxtCustomerName.Text))
{
CustomerDetaile.CustomerName = TxtCustomerName.Text;
}
CustomerDetaile.CustomerLastName = TxtLastName.Text;
CustomerDetaile.CustomerAdresse = TxtCustomerAdress.Text;
CustomerDetaile.CustomerCellPhone = Convert.ToInt32(TxtCustomerCellphone.Text);
CustomerDetaile.CustomerCode = Convert.ToInt32(TxtCustomerCode.Text);
context.SaveChanges();
}
非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.