ADO.NET实体框架SaveChanges()不起作用

.net c# entity-framework linq

我想在我的数据库中添加新的管理员。但它不起作用。请查看button1_Click事件处理程序,我需要在此事件中添加值。

public partial class Form1 : Form
    {
        protected NetTanitimTestEntities adminNameContext;
        public Form1()
        {
            InitializeComponent();
            adminNameContext = new NetTanitimTestEntities();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ObjectQuery<Admins> adminNameQuery = adminNameContext.Admins;
            dataGridView1.DataSource = adminNameQuery;
            dataGridView1.Columns["id"].Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            using (NetTanitimTestEntities newadmin = new NetTanitimTestEntities())
            {
                Admins admin = new Admins { Name = "ali", SurName = "Çorlu", Username = "acorlu", Password = "1234", UserType = "user" };
                newadmin.SaveChanges();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            adminNameContext.SaveChanges();
        }
    }

热门答案

看起来您需要在代码中添加以下命令:

using (NetTanitimTestEntities newadmin = new NetTanitimTestEntities())
{
     Admins admin = new Admins { Name = "ali", SurName = "Çorlu", Username = "acorlu", Password = "1234", UserType = "user" };
     newadmin.AddToAdmins(admin);
     newadmin.SaveChanges();
}

或类似的东西,取决于生成的方法的实际名称。您的“newadmin”对象应该为模型中的每个实体生成一堆“添加”方法。




许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因