实体框架不添加到数据库 [英] Entity Framework Doesn't add to the Database

查看:77
本文介绍了实体框架不添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架使用Windows窗口注册客户但是在调用add方法并调用savechange方法之后,该行没有插入,我不知道为什么这里是我的代码:有一个名为的主键列Cid是身份



这是一个注册表格,当用户点击注册按钮的主表单时,此表单加载并输入他的数据并按提交按钮。但是,当我单击提交按钮后,它不会在调用.SaveChanges()后添加到数据库中我关闭我的表单并查看来自服务器资源管理器的数据(R-单击表格并显示表格数据)是空的。甚至我尝试在寄存器表单中放置一个断点,看看'db'是否有任何数据都没有



这里是我的代码:



Im using entity framework to register a customer using Windows Form but after calling the add method and calling the savechange method the row doesn't insert and i don't know why here is my code: there is a primary key column named Cid which is identity

it is a register form when ever from the main form the user click on the register button this form loads and he enters his data and press the submit button.but when i click the submit button it doesn't add to database after the call to .SaveChanges() I close my forms and see the data from the server explorer (R-click on the tables and show table data) and it is empty.and even I tried putting a breakpoint in the register form and see if the 'db' has any data which is none

here is my code:

public partial class RegisterForm : Form
{
    PachinEntities1 db=new PachinEntities1();
    private Customer temp;
    public RegisterForm()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();  
    }

    private void RegisterForm_Load(object sender, EventArgs e)
    {
        temp=new Customer();
        DateTime gocal = DateTime.Now;
        PersianCalendar pcal=new PersianCalendar();
        int year=pcal.GetYear(gocal);
        int month = pcal.GetMonth(gocal);
        int day = pcal.GetDayOfMonth(gocal);
        string pday = string.Format("{0}/{1}/{2}", year, month, day);
        temp.Date = pday;
        txt_Date.Text = pday;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (txt_Name.Text.Trim().Length != 0)
        {

            if (txt_Tell1.Text.Trim().Length != 0)
            {
                if (txt_Address.Text.Trim().Length != 0)
                {
                    temp.Name = txt_Name.Text;
                    temp.Tell1 = txt_Tell1.Text;
                    temp.Tell2 = txt_Tell2.Text;
                    temp.Address = txt_Address.Text;
                    db.Customers.Add(temp);
                    db.SaveChanges();
                }
                else
                {
                    MessageBox.Show("آدرس را وارد کنید");
                }
            }
            else
            {
                MessageBox.Show("شماره تلفن را وارد کنید");
            }
        }
        else
        {
            MessageBox.Show("نام را وارد کنید");
        }
    }
}

推荐答案

谢谢大家,但问题解决了。我用过使用statement而不是global来创建我的上下文的实例



Thank you everyone but my problem is solved.i used Using statement instead of globally make an instance of my context

using (var db = new PachinEntities())
                        {
                            temp.Name = txt_Name.Text;
                            temp.Tell1 = txt_Tell1.Text;
                            temp.Tell2 = txt_Tell2.Text;
                            temp.Address = txt_Address.Text;
                            db.Customers.Add(temp);
                            db.SaveChanges();
                        }


这篇关于实体框架不添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆