Can I specify child entity default ordering with Entity Framework Code First?

▼魔方 西西 提交于 2020-01-25 03:20:42

问题


For example, with the following classes

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

And context

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

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("");
        }

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


回答1:


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



来源:https://stackoverflow.com/questions/4741714/can-i-specify-child-entity-default-ordering-with-entity-framework-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!