我可以用Entity Framework Code指定子实体默认排序吗? [英] Can I specify child entity default ordering with Entity Framework Code First?

查看:200
本文介绍了我可以用Entity Framework Code指定子实体默认排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,使用以下类

public class Child
{
    public Guid Id { get; set; }
    public String Description { get; set; }
    public double Value { get; set; }

    public Child()
    {
        Id = Guid.NewGuid();
    }
}

public class Parent
{
    public Guid Id { get; set; }
    public String Name { get; set; }
    public virtual IList<Child> Children { get; set; }

    public Parent()
    {
        Id = Guid.NewGuid();
        Children = new List<Child>();
    }
}    

和上下文

public class TempContext : DbContext
{
    public DbSet<Child> Children { get; set; }
    public DbSet<Parent> Parents { get; set; }
}

如何确保Parent.Children中的对象是按Value

How could I ensure that the objects in Parent.Children are ordered by Value

        TempContext tc = new TempContext();            
        var parents = tc.Parents.ToList();

        foreach (var p in parents)
        {
            Debug.WriteLine("Parent : {0}", (object) p.Name);
            foreach (var c in p.Children)
            {
                Debug.WriteLine("Child : {0} - {1}", c.Value, c.Description);
            }
            Debug.WriteLine("");
        }

显然,我可以在迭代集合之前对p.Children进行排序, d喜欢收集已经被订购。

Obviously, I can sort p.Children above before iterating the collection but I'd like the collection to already be ordered.

推荐答案

你必须为此写一个查询。订单不受映射管理。

You must write a query for that. Ordering is not managed by mapping.

这篇关于我可以用Entity Framework Code指定子实体默认排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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