简体   繁体   中英

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:

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"

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM