有没有办法强制实体框架中相关数据的SortOrder? [英] Is there a way to force SortOrder on Related Data in Entity Framework?

查看:71
本文介绍了有没有办法强制实体框架中相关数据的SortOrder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有以下简化场景的EF Code-First实现:

  public class Form {
[key]
int FormID {get; set;}
int Name {get; set;}

public virtual ICollection< FormAndQuestionMapping> FormQuestionMappings
{
get;
设置;
}
}

public class FormAndQuestionMapping {
[ForeginKey]
int FormID {get; set;}

[ ForeginKey]
int QuestionID {get; set;}

int SortOrder {get; set;}

public virtual Form Form {get;组; }
public virtual问题问题{get;组; }
}

FormAndQuestionMapping数据:

  FormID | QuestionID | SortOrder 
1 | 1 | 5
1 | 2 | 20
1 | 3 | 10
1 | 4 | 15

有没有办法强制FormQuestionMappings由其SortOrder加载而不是默认位置?

解决方案

是的,这是可能的。
实体框架接受实现ICollection接口的所有集合。所以,你可以使用你自己的收藏,这是你想要的任何方式。
EF通过ICollection界面上的Add方法调用实现集合。所以,这样的东西应该可以工作:

  public class Form 
{
private ICollection< FormAndQuestionMapping> _formQuestionMappings;

public Form()
{
_formQuestionMappings = new SortedSet< FormAndQuestionMapping>(new Comparer());
}

私有类比较器:IComparer< FormAndQuestionMapping>
{
public int Compare(FormAndQuestionMapping x,FormAndQuestionMapping y)
{
return x.SortOrder - y.SortOrder;
}
}

[key]
int FormID {get;组; }
int Name {get;组; }

public virtual ICollection< FormAndQuestionMapping> FormQuestionMappings
{
get {return _formQuestionMappings; }
set {_formQuestionMappings = value;
}
}

public class FormAndQuestionMapping
{
[ForeginKey]
int FormID {get;组; }

[ForeginKey]
int QuestionID {get;组; }

public int SortOrder {get;组; }

public virtual Form Form {get;组; }
public virtual问题问题{get;组; }
}


Assuming that I have an EF Code-First implementation with the following simplified scenario:

public class Form{
    [key]
    int FormID {get;set;}
    int Name {get;set;}

    public virtual ICollection<FormAndQuestionMapping> FormQuestionMappings
    {
        get;
        set;
    }
}

public class FormAndQuestionMapping{
    [ForeginKey]
    int FormID {get;set;}

    [ForeginKey]
    int QuestionID {get;set;}

    int SortOrder {get; set;}

    public virtual Form Form { get; set; }
    public virtual Question Question { get; set; }
}

FormAndQuestionMapping data:

 FormID | QuestionID | SortOrder
   1    |     1      |     5
   1    |     2      |     20
   1    |     3      |     10
   1    |     4      |     15

Is there a way to force FormQuestionMappings to be loaded by their SortOrder and not by their default location?

解决方案

Yes, it's possible. Entity Framework accepts all collections, that implements ICollection interface. So, you can use your own collection, that is sorded in any way you want. EF materialize collections via Add method calls on ICollection interface. So, something like this should work:

public class Form
{
    private ICollection<FormAndQuestionMapping> _formQuestionMappings;

    public Form()
    {
        _formQuestionMappings = new SortedSet<FormAndQuestionMapping>(new Comparer());
    }

    private class Comparer : IComparer<FormAndQuestionMapping>
    {
        public int Compare(FormAndQuestionMapping x, FormAndQuestionMapping y)
        {
            return x.SortOrder - y.SortOrder;
        }
    }

    [key]
    int FormID { get; set; }
    int Name { get; set; }

    public virtual ICollection<FormAndQuestionMapping> FormQuestionMappings
    {
        get { return _formQuestionMappings; }
        set { _formQuestionMappings = value; }
    }
}

public class FormAndQuestionMapping
{
    [ForeginKey]
    int FormID { get; set; }

    [ForeginKey]
    int QuestionID { get; set; }

    public int SortOrder { get; set; }

    public virtual Form Form { get; set; }
    public virtual Question Question { get; set; }
}

这篇关于有没有办法强制实体框架中相关数据的SortOrder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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