我想知道在这种情况下是否有人可以帮助我:我试图将我的更改保存到数据库,所以我使用了一个上下文,我有_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();
}
但我不知道为什么它还没有保存......
提前致谢。
答案 0 :(得分:4)
我没有看到你在上下文中添加_tblCustomer对象的任何地方,就像在你的“主要部分”中那样
context.tblCustomers.Add(_tblCustomer);
如果您想要修改现有对象,则应改为编写
CustomerDetaile.CustomerId = "the new id"
现在它将被保存。
您现在正在做的是创建一个新客户,分配其价值并不做任何事情。
答案 1 :(得分:0)
在这种情况下,要将新对象保存到数据库,您需要使用:
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();
它总是对我有用=)将这个概念应用到你的代码中它可能会起作用。
答案 2 :(得分:0)
我不应该做一个新的...我必须为我的选择编写代码...我通过@ChriskKouamé得到它帮助这里是我的代码:
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();
}
非常感谢。