vendredi 24 juin 2016

DataGridView shows excess columns with datasource and entity framework [duplicate]

This question already has an answer here: How to map properties of inner/nested classes in DataGridView through BindingSource? 3 answers I use entity framework to bind data to DataGridView. So I have a class generated from table of MySql database. public partial class groups { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public groups() { this.students = new HashSet<students>(); } public bool has_schedule { get; set; } public string group_id { get; set; } public int semester_id { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<students> students { get; set; } public virtual semesters semesters { get; set; } } Table groups references table semesters and is referenced by table students. I bind groups to groupsDataGridView1 using (var mc = new Model1Container1()) { try { mc.Database.Connection.Open(); this.groupsDataGridView1.DataSource = mc.groups.Include("students").ToList(); mc.Database.Connection.Close(); } catch (MySql.Data.MySqlClient.MySqlException ex) { Console.WriteLine(ex.Message + 'n' + ex.ToString()); } } I have to add Include method. Without it I get exception. I've read about lazy loading. But there are no columns from table 'students'. There should be loading semesters. So I don't understand this logic. Also groupsDataGridView1 behaves strange. Beside groups columns it shows column students and semesters (names of tables, not contained columns). As I found out I can hide column with attribute [System.ComponentModel.Browsable(false)] But is it normal to add manually this attribute to all table generated classes which has references to another tables or are referenced by others?

Aucun commentaire:

Enregistrer un commentaire