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

查看:24
本文介绍了如何将实体框架 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?

我想另一种选择是让包含所选客户对象的 VM 还包含一个本地 ObservableCollection,它在选择客户对象时创建......我只是担心上下文保存并保持数据同步.

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.

与子对象集合关联的典型代码生成对象:

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; }
}

推荐答案

这就是我所做的,并且首先使用 EF 数据库对我有用.

That's what I did and what works for me with EF Database first.

这就是你需要生成的:

public partial class Parent
{
    public Parent()
    {
        this.Children= new ObservableCollection<Child>();
    }

这样默认的构造函数将被替换.而 ObservableCollection 就是 ICollection,所以你不需要改变任何其他东西.

So that default costructor will be replaced. And ObservableCollection is ICollection, so you don't need to change anything else.

要在每次更新数据库模型时都显示此内容,您必须使用以下部分更改 .tt 文件:

To make this appear every time you update your database model you have to change your .tt file with following parts:

public string UsingDirectives(bool inHeader, bool includeCollections = true)
{
    return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
        ? string.Format(
            CultureInfo.InvariantCulture,
            "{0}using System;{1}" +
            "{2}",
            inHeader ? Environment.NewLine : "",
            includeCollections ? (Environment.NewLine + "using System.Collections.ObjectModel;" 
                + Environment.NewLine + "using System.Collections.Generic;") : "",
            inHeader ? "" : Environment.NewLine)
        : "";
}

<小时>

还有这个:


and this:

    foreach (var navigationProperty in collectionNavigationProperties)
    {

    this.<#=code.Escape(navigationProperty)#> = new ObservableCollection<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
    }

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

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