SaveChanges() in ADO.NET Entity Framework does not function.

.net c# entity-framework linq

Question

I want to add new admin into my database. But it doesn't work it. Please look at the button1_Click event handler, I need to add value in this event.

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();
        }
    }
1
1
6/13/2009 11:55:03 AM

Popular Answer

It looks like you need to add the following command into you code:

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

or something like that depending on the actual name of the generated method. Your "newadmin" object should have a bunch of "add" methods generated for each entity you have in your model.

13
6/13/2009 11:12:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow