Hello, I am trying to bind a WinGrid to my simple Entity Framework CodeFirst data. I created a few simple models, a DBContext, an implementation of a generic Repository Pattern and a UnitOfWork DataaccessLayer to access my database.
But I'm having problems binding the grid to it, as all the datasources somehow do not work and the EntityDataSource does not exist anymore in the latest EF/.NET version. The only way that seems to work, is to surpass the repository and access the DbSet directly:
grid.DataSource = dal.getContext().Students.Local.ToBindingList();
dal is an object of a class containing all the repositories, and getContext returns the DBContext containing all the DbSet's.
It works, but it feels rather....dirty. Usually, the Entity Framework offers such amazing and simple features, but binding to Controls (not only infragistics) seems to be a huge pain. Is there really no way to use visual binding and automatically generated Data/column schemes and all that stuff I had with old ado.net datasets?
Hello Kevin,
When using Entity Framework Code First you can create visual binding by following next steps in Visual Studio 2013 (as you would do this for any other component):
On Main Menu select Project -> Add New Data Source.
Select Object and click ‘Next’.
In Select the Data Objects select the necessary objects from your Data First Model
Click Finish.
Show Data Sources (Main Menu -> View -> Other Windows -> Data Sources).
Select the necessary data source class and drag and drop it over the UltraGrid. This way UltraGrid is bound to the binding source.
Please follow the next link for more information regarding Entity Framework Code First data biding http://blogs.msdn.com/b/adonet/archive/2011/02/16/ef-feature-ctp5-code-first-and-winforms-databinding.aspx
Please note whenever way you bind the data all the changes need to be populate to database with DbContex.SaveChanges() method.
Please let me know if this is what you are looking for or if I am missing something.
Hi Milko, thanks a lot for the link. I did try it exactly as in the link, and everything works except that if I want to add a row in the Children data (Band 1), it only inserts them into the database if I add at least 2 rows at the same time. If I add only one child row, it does not save it to the database.
What can that be?
To provide some code:
private void bindGrid(){
dal.dbContext.Websites.Load();
ultraGrid2.DataSource = dal.dbContext.Websites.Local.ToBindingList();
ultraGrid2.DataBind();
}
private void saveButton_Click(object sender, EventArgs e){ this.Validate(); foreach (var alias in dal.dbContext.Aliases.Local.ToList()) {
if (alias.Website == null)
{
dal.dbContext.Aliases.Remove(alias);
dal.dbContext.SaveChanges();
dal.Save();
ultraGrid2.Refresh();
My Website model Looks like
public class Website {
private readonly ObservableListSource<Alias> _aliases = new ObservableListSource<Alias>();
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Name { get; set; }
public string URL { get; set; }
public virtual ObservableListSource<Alias> Aliases { get { return _aliases; } }
public class Alias {
public int WebsiteID { get; set; }
public virtual Website Website { get; set; }
public string AliasName { get; set; }
I am just checking about the progress of this issue. Let me know if you need my further assistance on it.
Thank you for using Infragistics Components.