C# 如何使用实体框架将更改保存到数据库?

C# 如何使用实体框架将更改保存到数据库?,c#,entity-framework,C#,Entity Framework,我想知道是否有人能在这种情况下帮助我:我正在尝试保存对数据库的更改,所以我使用了上下文,我有\u tblcustomer,它是我实体类中的一个对象,下面是我的代码: private void BtnSaveCustomer_Click(object sender, EventArgs e) { if (CustomerMode == (int)CustomerModeOperaton.insert) { if (!string.IsNullOrWhiteSpace

我想知道是否有人能在这种情况下帮助我:我正在尝试保存对数据库的更改,所以我使用了上下文,我有
\u 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);
                   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();
                }
主要部分如下:

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();
}
但我不知道为什么它还没有保存


提前感谢。

我看不到您在上下文中添加“客户”对象的任何地方,以及在“主要部分”中添加类似的内容

如果要修改现有对象,则应改为编写

CustomerDetaile.CustomerId = "the new id"
现在它将被拯救


你现在所做的是创建一个新客户,分配其价值,而不做任何事情

我看不到在上下文中添加_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();

它始终适用于我=)将此概念应用到您的代码中,它可能会起作用。

在这种情况下,要将新对象保存到数据库,您需要使用:

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éhelp获得的这是我的代码:

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);
                   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();
                }

非常感谢。

我不应该做新的。。。我必须为我的选择写代码。。。我是通过@ChristianKouaméhelp获得的这是我的代码:

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);
                   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();
                }

非常感谢。

context.SaveChanges()应该这样做,不是吗?@jdmdevdotnet是的,先生。。但它不起作用“它不起作用”什么不起作用<代码>上下文。保存更改
有效。什么错误消息?@jdmdevdotnet没有错误消息,context.SaveChanges应该将我的更改保存到数据库中,例如,我用以下代码更改客户姓氏_tblCustomer.CustomerLastName=TxtLastName.Text;但是它不会保存在我的数据库中如果你想修改姓氏,你应该改为写customerDetail.CustomerLastName=TxtLastName.Text.
context.SaveChanges()应该这样做,不是吗?@jdmdevdotnet是的,先生。。但它不起作用“它不起作用”什么不起作用<代码>上下文。保存更改
有效。什么错误消息?@jdmdevdotnet没有错误消息,context.SaveChanges应该将我的更改保存到数据库中,例如,我用以下代码更改客户姓氏_tblCustomer.CustomerLastName=TxtLastName.Text;但是它不会保存在我的数据库中如果你想修改姓氏,你应该改为写customerDetail.CustomerLastName=TxtLastName.Text。因为我不应该使用此代码。。。我的数据库中有这个客户,为什么我要再次添加它!?如果添加,我将面临以下错误:无法在对象“dbo.tblCustomers”中插入重复键在这种情况下,您必须修改从数据库中获取的实体,而不是创建新实体(我编辑了答案),因为我不应该使用此代码。。。我的数据库中有这个客户,为什么我要再次添加它!?如果我添加,我将面临以下错误:无法在对象“dbo.tblCustomers”中插入重复键在这种情况下,您必须修改从数据库中获取的实体,而不是创建新实体(我编辑了答案)我没有获取Entrystate,Entrystate在我的代码中是什么?不在您的代码中,只是为了将来,如果您想更改现有对象中的某些内容,则必须键入该内容;obj.Name=“BLA”;目标工资=32;context.Entry(obj.State=EntryState.Modified;SaveChanges();我没有得到Entrystate,什么是我的代码中的Entrystate?不是在你的代码中,只是为了将来如果你想更改现有对象中的某些内容,你必须键入它。例如,var obj=context.TableName.Find(id);obj.Name=“BLA”;目标工资=32;context.Entry(obj.State=EntryState.Modified;SaveChanges();