[英]How save my changes to database with Entity Framework?
I was wondering if someone can help me in this case: I'm trying to save my changes to database, so I use a context, and I have _tblcustomer
which is an object from my entity classes, here is my code: 我想知道在这种情况下是否有人可以帮助我:我试图将所做的更改保存到数据库中,所以我使用了上下文,并且我有
_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("اطلاعات در سیستم ثبت شد");
}
}
Main part is here: 主要部分在这里:
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();
}
but I don't know why it does not save yet... 但我不知道为什么它还不能保存...
Thanks in advance. 提前致谢。
I don't see anywhere you add the _tblCustomer object in the context, with something like in your "main part" 我看不到您在上下文中添加_tblCustomer对象的任何地方,例如在“主要部分”中
context.tblCustomers.Add(_tblCustomer);
If instead you want to modify an exiting object, you should write instead 相反,如果您要修改现有对象,则应编写
CustomerDetaile.CustomerId = "the new id"
And now it will be saved. 现在它将被保存。
What you are doing now, is creating a new customer, assigning its values and do nothing with it. 您现在正在做的是创建一个新客户,为其分配值,而不对其进行任何处理。
To save new object to database in this case you would need to use: 在这种情况下,要将新对象保存到数据库,您将需要使用:
var obj = context.TableName.New();
obj.Name = "BLA";
obj.Salary = 32;
context.TableName.Add(obj);
context.SaveChanges();
To edit existing object in the table: 要编辑表中的现有对象:
var obj = context.TableName.Find(id);
obj.Name = "BLA";
obj.Salary = 32;
context.Entry(obj).State = EntryState.Modified;
context.SaveChanges();
It always worked for me=) Apply this concept to your code and it may work. 它始终对我有用=)将这个概念应用于您的代码,它可能会起作用。
I shouldn't make a new ... I have to write code for my select... I got it by @ChristianKouamé help here is my code: 我不应该做一个新的...我必须为自己的选择编写代码...我通过@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();
}
thanks a lot. 非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.