将DataGridView绑定到具有EF5类的数据库,而不会向数据库发送更改 [英] Binding DataGridView to database with EF5 classes not sending changes to database
问题描述
我已经使用EF Model第一种方法创建了数据库,我想绑定 DataGridView
到使用EF类的数据库,以便数据库或 DataGridView
中的任何更改都将自动同步。
这是我的代码:
//表单级别字段
private BindingList< ;产品> _产品;
private BindingSource _productSource = new BindingSource();
...形式加载事件
//使用EF类从数据库加载数据
var tmp = _context.BaseCategorySet.OfType< Product>()。ToList( );
//转换为IBindingList
_products = new BindingList< Product>(tmp);
_products.AllowEdit = true;
_products.AllowNew = true;
_productSource.DataSource = _products;
//设置GridControl的数据源
ProductGrid.DataSource = _productSource;
我可以添加新行或更改数据,但是这些更改不会发送到数据库 - 什么是我错过了吗?
我希望找到解决方案的其他事情...
1)我添加了一个保存按钮,用于显式地将网格控件的数据更新到数据库,代码如下:
_productSource。 EndEdit();
_context.SaveChanges();
- >这不会导致将新记录存储到数据库中
2)我添加了一个代码来添加一些个别记录属性控件的新记录(Textboxes,DatePickers)
var x = _context.BaseCategorySet.Create< Product>();
//设置所有x属性,值为
//,设置在上述各个控件中
_context.BaseCategorySet.Add(x);
_context.SaveChanges();
- >当我使用这种技术添加新记录时 - 它存储在数据库中,但一次更奇怪的一个行为 - 这个新记录不会自动加载到网格控件中(但应该是,因为我将数据绑定到相应的EF DbSet ...)
还有一个更奇怪的是 - 我在 DataGridView
控件中对加载到数据库的记录进行更新 - 这些更新被发送到数据库...
XtraGrid
切换到stadard DataGridView
控件,但是没有帮助... 我已经搜索了关于EF数据绑定的大量主题,没有成功...
不知道这是否重要,但我在实体模型中使用继承:产品
源自 UnifOfSales
和 UnitOfSales
派生自 BaseCategory
类。
还有一件事ried
我尝试了(..)。Ladislav Mrnka在这篇文章中建议的Local.ToBindingList
如何在winform中使用EF进行双向数据绑定?
它将更改发送回数据库,但更改仅存储在基类表(BaseCategory)中,但派生类也有一个表。这是我用于绑定的代码
_context.BaseCategorySet.OfType< Product> .Load();
//我试图使用派生类与OfType< Product>确保编译器
//知道这是派生类(Product)的实例,
//不是基类BaseCategory,
//但是我不能得到本地使用OfType ...
ProductGridView.DataSource = _context.BaseCategorySet.Local.ToBindingList();
我找到了解决方案!
我不得不在DbContext类中为EF模型派生类添加DbSet产品(Product,派生自BaseCategory类)
此后我可以使用
ProductGridView.DataSource = _context.Products.Local.ToBindingList();
I am using Entity Framework 5.0 + .NET 4.5
I have created database using EF Model first approach and I want to bind DataGridView
to database using EF classes so that any changes to database or in the DataGridView
are synchronized automatically.
This is my code:
//form level fields
private BindingList<Product> _products;
private BindingSource _productSource = new BindingSource();
... in the form load event
//load the data from database using EF classes
var tmp = _context.BaseCategorySet.OfType<Product>().ToList();
//converting to IBindingList
_products = new BindingList<Product>(tmp);
_products.AllowEdit = true;
_products.AllowNew = true;
_productSource.DataSource = _products;
//setting GridControl's data source
ProductGrid.DataSource = _productSource;
I can add new rows or change data, but those changes are not sent to the database - what am I missing?
Additional things I did in hope to find a solution...
1) I added a Save button to call updating the grid control's data to database explicitly, with code:
_productSource.EndEdit();
_context.SaveChanges();
->this did not result in storing the new record into the database
2) I added a code to add new records with a bunch of controls for individual record properties (Textboxes, DatePickers)
var x = _context.BaseCategorySet.Create<Product>();
//settting all the x properties with values
//that are set in aforementioned individual controls
_context.BaseCategorySet.Add(x);
_context.SaveChanges();
-->when I add new records using this technique - it IS stored in the database, but once more a strange behavior - this new record is not automatically loaded in the grid control (but should be, since I am databinding grid to the corresponding EF DbSet...)
And one more strangeness - updates that I have made in the DataGridView
control to the records that were loaded to database - those updates ARE sent to database...
3) I switched from DevExpress XtraGrid
to stadard DataGridView
control, but that did not help...
I have searched through tons of topics regarding EF databinding with no success...
Don't know if this matters, but I am using inheritance in my Entity model: Product
derives from UnifOfSales
and UnitOfSales
derives from BaseCategory
class.
One more thing i tried
I tried the (..).Local.ToBindingList that was suggested by Ladislav Mrnka in this post How to make two way-databinding using EF in winforms?
well it did send changes back to the database, but the changes were stored only in the base class table (BaseCategory), but there is a table for derived class as well. This is the code i used for binding
_context.BaseCategorySet.OfType<Product>.Load();
//i tried to use derived class with OfType<Product> to ensure that compiler
//knows that this is instance of derived class (Product),
//not the base class BaseCategory,
//but I can not get "Local" working with OfType...
ProductGridView.DataSource = _context.BaseCategorySet.Local.ToBindingList();
I did find the solution!
I had to add DbSet Products in DbContext class for EF model derived class (Product, derived from BaseCategory class)
After this I could use
ProductGridView.DataSource=_context.Products.Local.ToBindingList();
这篇关于将DataGridView绑定到具有EF5类的数据库,而不会向数据库发送更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!