Reputation: 2234
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.
Upvotes: 0
Views: 7442
Reputation: 758
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.
Upvotes: 0
Reputation: 4046
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.
Upvotes: 0
Reputation: 1319
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
Upvotes: 1
Reputation: 5832
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.
Upvotes: 2