如何将实体框架ICollection更改为ObservableCollection? [英] How can I change an Entity Framework ICollection to be an ObservableCollection?

查看:171
本文介绍了如何将实体框架ICollection更改为ObservableCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在使用实体框架设计器的兔子洞里做得很好,使得一个作为MVVM项目中的模型的EDMX。
我刚刚遇到一个问题,我很确定那个代码生成的 ICollection<> 真的需要作为一个 ObservableCollection<> ,用于将该集合绑定到视图中成功的 DataGrid
我认为我有一些点击修改EF代码生成的可能性,使 ObservableCollections 而不是 ICollections 。任何一个曾尝试过成功的人?

So I'm pretty far down the rabbit hole of using the Entity Framework designer to make an EDMX that serves as the model in an MVVM project. I've just come across an issue where I'm pretty sure that the ICollection<> that was code generated (see below for example) really needs to be an ObservableCollection<> for binding that collection to a DataGrid in a view to be successful. I think I'm getting some hits on the possibility of modifying the EF code generation to make ObservableCollections rather than ICollections. Any one ever tried that successfully?

我想另外一个选项是让包含所选Customer对象的VM也包含一个本地的 ObservableCollection< Order> ; 在客户对象被选择时被创建....我只是担心上下文保存并保持数据同步。

I suppose another option would be have the VM that contains the selected Customer object also contain a local ObservableCollection<Order> that gets created when the Customer object is selected....I just worry about the context saves and keeping the data in sync.

具有与子对象集合关联的典型代码gen对象:

typical code gen object with an association to a collection of child objects :

    public partial class Customer
{
    public Customer()
    {
        this.Orders = new HashSet<Order>();
    }

    public int Id { get; set; }
    public System.DateTime Date { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}


推荐答案

您的数据逻辑模型应该与您的 viewmodel 模型。所以,我认为更好的选择是你在创建一个 ObservableCollection 时所说的。保存时,您可以随时同步到上下文(我忘记了要同步的确切语法)。

Your data logic and models should be separate from your viewmodel and models. So, I would think that the better option is what you are talking about in creating an ObservableCollection. You can always sync to the context (I forget the exact syntax to sync) when you save.

这篇关于如何将实体框架ICollection更改为ObservableCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆