简体   繁体   中英

Entity Framework insert doesn't work

I have a very simple windows form project with Entity Framework.

Simply I draged my tables from "Data Source" tab in my "form" and it generate for me a "DataGridView" and a "BindingSource".

Data bound successfully and when I run project I can see "DataGridView" filled with data correctly and I can update any cells value from "DataGridView".

Problem is I can not insert any rows in my "DataGridView".

Here is some codes that I wrote hope it be useful to solve problem:

    Teachers_DBEntities context = new Teachers_DBEntities();

    private void SaveItem_Click(object sender, EventArgs e)
    {
        context.SaveChanges();
        MessageBox.Show("saved successfully");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        teachers_tableBindingSource.DataSource = context.teachers_table.ToList();
    }

Upates for comments

I tested my "BindingSource" and found that it successfully understand if new record insert in "DataGridVeiw", but changes won't apply in database when I call context.savechanges();

context.savechanges() works fine when I update a cell, but it doesn't work when I try to insert a new record in my "DataGridView".

In my edmx file I mapped all columns correctly and primary key StoreGeneretedPattern property is set to Identity and its Entity Key property is set to true . Its auto-increment property in my SQL Server database file is set to true .

any help is appreciated.

context.savechanges() works fine when I update a cell, but it doesn't work when I try to insert a new record in my "DataGridView".

What do you mean by "it doesn't work"? New record does not appear in database? Of course it won't.

In this line

teachers_tableBindingSource.DataSource = context.teachers_table.ToList();

you're breaking DataSource 's connection to context. Any new item inserted into it will be inserted not into teachers_table , but into List you created over it.

using (var context = new YourEntities ()) 
{
     var dpt = new yourObject { Name = "Test" };
     context.yourObject.Add(dpt);
     context.SaveChanges(); 
}

you are missing to add object in the context

In .Net 4.5 I get this error when I remove ToList():

Data binding to a store query is not supported.

Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data.

I had exactly this same problem. And after a lot of hours, I just finally found myself the solution...

I'm using C#, .Net Framework, Entity Framework 6.4

If I set

teachers_tableBindingSource.DataSource = context.teachers_table.ToList();

data is updated in DB but not inserted.

If I set (as suggested)

teachers_tableBindingSource.DataSource = context.teachers_table;

I get the error:

System.NotSupportedException HResult=0x80131515 Mensaje = Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592 .

Finally I got the answer tnanks to that hint:

context.teachers_table.Load(); //Load records in this DataSet<>

teachers_tableBindingSource.DataSource = context.teachers_table.Local.ToBindigngList();

Now it works. Data is deleted, inserted and updated properly in database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM