开发者

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

开发者 https://www.devze.com 2023-02-06 04:01 出处:网络
For example, with the following classes public class Child { public Guid Id { get; set; } public String Description { get; set; }

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.Child开发者_运维百科ren 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消