关于实体框架的问题 [英] Question about entity framework

查看:89
本文介绍了关于实体框架的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我对实体框架实体模型有一点疑问。实体框架使用ICollection创建模型的导航属性。但由于索引,我需要List。



我的问题是;我可以将这些属性更改为如下所示的列表吗?



谢谢。



我的尝试:



Hi,

I have a little question about entity framework entity models. The entity framework creates model's navigation properties with ICollection. But I need List's because of indexing.

My question is; Can I change these properties to Lists like below?

Thank you.

What I have tried:

public partial class Student
{
    public Student()
    {
        Courses = new HashSet<Course>();
    }

    public int ID;
    public string Name;

    public virtual ICollection<Course> Courses { get; set; }
}





to





to

public partial class Student
{
    public Student()
    {
        Courses = new HashSet<Course>();
    }

    public int ID;
    public string Name;

    public virtual IList<Course> Courses { get; set; }
}

推荐答案

使用 IList< T> 将有效很好,虽然索引到列表中并没有多大意义,因为相关实体不会有预先定义的订单。



但是, HashSet< T> 未实现 IList< T> ,因此您必须选择其他类 - 列出< T> 或许。
Using IList<T> will work fine, although it doesn't really make sense to index into the list, since there won't be a pre-defined order for the related entities.

However, HashSet<T> doesn't implement IList<T>, so you'll have to pick a different class - List<T> perhaps.


这篇关于关于实体框架的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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