首页 > 解决方案 > 防止在数据网格中加载引用的表格

问题描述

使用数据库中的一张表填充 WAF 中的数据网格时出现问题。在我的数据库中,我创建了几个引用一个特定列 (Id_Exhibit) 的表。

在使用所需表填充我的数据网格期间,一切看起来都很好,但其他引用的表也会填充它。

在我选择的所有列都填充了来自数据库中特定表的数据之后,就会出现另一个具有引用 Id_Exhibit 列的表的名称...

如何防止它们加载到数据网格中?

我的代码中没有太多内容,只有一种填充数据网格的方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MuzeumGUI
{
    public partial class Exhibits: Form
    {

        public Exhibits()
        {
            InitializeComponent();

        }

        private void Ehxibits_Load(object sender, EventArgs e)
        {
            PopulateDataGridView();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        void PopulateDataGridView()
        {
            using (DBEntities db = new DBEntities())
            {
                dgvExhibits.DataSource = db.DBExhibit.ToList();
            }

        }
    }
}

至于从我创建的实体生成的类,Exhibit 如下所示:

namespace MuzeumGUI
{
    using System;
    using System.Collections.Generic;

    public partial class DBExhibit
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public DBExhibit()
        {
            this.DBRenovation = new HashSet<DBRenovation >();
            this.DBReservation= new HashSet<DBReservation>();
            this.DBRental= new HashSet<DBRental>();
        }

        public int Id_Exhibit { get; set; }
        public string Name{ get; set; }
        public string Era{ get; set; }
        public string Description{ get; set; }
        public string Creator{ get; set; }
        public string Type{ get; set; }
        public Nullable<int> Year { get; set; }
        public string Century { get; set; }
        public string Style { get; set; }
        public string Status{ get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ICollection<DBRenovation> DBRenovation{ get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ICollection<DBReservation> DBReservation{ get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ICollection<DBRental> DBRental{ get; set; }
    }
}

标签: c#entity-framework

解决方案


推荐阅读