Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将EntityFramework生成的数据库集与网格上的DataContext一起使用? 问题:_C#_Wpf_Entity Framework_Data Binding_Entity Framework 6 - Fatal编程技术网

C# 如何将EntityFramework生成的数据库集与网格上的DataContext一起使用? 问题:

C# 如何将EntityFramework生成的数据库集与网格上的DataContext一起使用? 问题:,c#,wpf,entity-framework,data-binding,entity-framework-6,C#,Wpf,Entity Framework,Data Binding,Entity Framework 6,我有一个EntityFramework生成的数据库集,我从Grid.DataContext绑定到该数据库集 myEntities = new MyEntities(); grMain.DataContext = myEntities.GalleryLists.ToList(); 所有这些都是宏大而华丽的,但列表并不是一个可观察的集合。我实际上(可能)想这样做: grMain.DataContext = myEntities.GalleryLists.Local; //myEntities.Ga

我有一个EntityFramework生成的数据库集,我从Grid.DataContext绑定到该数据库集

myEntities = new MyEntities();
grMain.DataContext = myEntities.GalleryLists.ToList();
所有这些都是宏大而华丽的,但列表并不是一个可观察的集合。我实际上(可能)想这样做:

grMain.DataContext = myEntities.GalleryLists.Local; //myEntities.GalleryLists //???;
但我非常希望使用的后一个选项在这个网格中为我提供了空的列表框,而前一个选项工作得非常好。那么我做错了什么

更多细节 网格使用列表框按以下方式定义(设置为绑定到库列表):


实体框架生成的对象如下所示:

public partial class TiresiasEntities : DbContext
{
    public TiresiasEntities()
        : base("name=TiresiasEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<GalleryItem> GalleryItems { get; set; }
    public virtual DbSet<GalleryList> GalleryLists { get; set; }
}
public partial class Gallery
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public GalleryList()
    {
        this.GalleryItems = new HashSet<GalleryItem>();
    }

    public int GalleryID { get; set; }
    public string GalleryName { get; set; }
    public byte IsSystemGallery { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<GalleryItem> GalleryItems { get; set; }
}
public部分类TiresiasEntities:DbContext
{
公共轮胎实体()
:base(“名称=轮胎实体”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共虚拟数据库集GalleryItems{get;set;}
公共虚拟数据库集GalleryLists{get;set;}
}
公共半类画廊
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共美术馆()
{
this.GalleryItems=new HashSet();
}
public int GalleryID{get;set;}
公共字符串GalleryName{get;set;}
公共字节IsSystemGallery{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection GalleryItems{get;set;}
}

感谢您的建议。

在绑定到DbSet.Local之前,您可能需要调用IQueryable.Load()

myEntities = new MyEntities();
myEntities.GalleryLists.Load();
grMain.DataContext = myEntities.GalleryLists.Local;

您不应该将业务对象直接绑定到UI。创建一个合适的BL和ViewModels来绑定这些对象。感谢您的评论。我知道这一点,但我只是在学习MVVM,请相信我,这还不是任何生产代码。问题在于如何实现这一结果。或者,正是为了让我理解为什么列表可以作为DataContext工作,而ObservableCollection不能。它们都实现了IEnumerable和IList(后者通过集合实现),我认为这是必需的,那么是什么让它们有所不同呢?正如@echo建议的,我可能丢失了数据加载?但是为什么.ToList()会自己加载数据呢?DbSet条目是IQueryable,因此不包含任何数据。它确实为linq方法实现了IEnumerable,但是它只包含查询/筛选信息。例如,通过.Load扩展方法或通过.ToList()(到另一个列表实例)枚举此IQueryable将通过执行查询来加载数据(请参阅linq中的延迟执行)。ObservableCollection应该作为绑定的源,并且您应该能够通过列表创建ObservableCollection。不过,将上下文图直接绑定到视图是一种糟糕的做法。好了,现在我要重读关于集合的一章。多亏了你的解释,我想我现在明白了为什么这个收藏是空的。谢谢。很抱歉,此对象上没有Load()这样的函数(也没有.Local属性上的函数)。或者我遗漏了什么?另外,为什么ToList()在没有事先调用Load()函数(如果有)的情况下工作,它是一个类型转换函数,它不应该进行任何数据加载,或者确实如此?请参阅MSDN中关于Load extension方法的这篇文章,并按照链接了解在处理本地数据时为什么可能使用它。Load是一种扩展方法,您需要导入System.Data.Entity名称空间谢谢,这很有效。我错过了Intellisense灯泡建议添加此名称空间。。。
myEntities = new MyEntities();
myEntities.GalleryLists.Load();
grMain.DataContext = myEntities.GalleryLists.Local;