我有一个数据库(具体来说是 Firebird,但我相信这个问题与 EF 有关)。我已经使用数据库中的 Code First 生成了代码文件。例如,为驱动程序生成的代码是:
[Table("Firebird.DRIVER")]
public partial class DRIVER
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public DRIVER()
{
FAREs = new HashSet<FARE>();
//..... and other code generated for the other collections
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int DRIVERID { get; set; }
[StringLength(100)]
public string FULLNAME { get; set; }
//... and other properties
public int? EQUIPMENTID { get; set; }
public virtual EQUIPMENT EQUIPMENT { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<FARE> FAREs { get; set; }
//.... and other code generated for the other collections
}
我有一个带有 DataGridView 的基本表单。DataGridView 的目的是编辑、添加或删除新实体(驱动程序)。我这样绑定
private async Task BindGrid()
{
var DBContext = AppVariables.CreateContext();
await Task.Run(() => DBContext.DRIVERs.Load());
var bindingSource = DBContext.DRIVERs.Local.ToBindingList();
dataGridView1.DataSource = bindingSource;
}
但是,我在我的网格中获得了导航属性(例如设备)。我该怎么做才能从加载网格中删除导航属性。我应该先从代码中删除导航属性吗?还是有另一种方法可以在不加载导航属性的情况下将数据加载到网格中?
我尝试仅使用属性创建对象并避免导航,但网格不可编辑
private async Task BindGrid()
{
var list = from driver in DBContext.DRIVERs
select new
{
DRIVERID = destination.DRIVERID
//...
};
await list.LoadAsync();
var bindingSource = list.ToBindingList();
dataGridView1.DataSource = bindingSource;
}
当年话下
相关分类