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”對象應該為模型中的每個實體生成一堆“添加”方法。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因