问题描述
我要创造我的ASP.NET MVC Web应用程序名为 Movie_Type
一个新的模型对象。会有什么区别,如果我定义这个类的导航proprty是不是列表
,的ICollection
或的IQueryable
如下?
I want to create a new model object named Movie_Type
in my ASP.NET MVC web application. What will be the differences if I define the navigation proprty of this class to be either List
, ICollection
or IQueryable
as following?
public partial class Movie_Type
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Movie> Movies { get; set; }
}
或
public partial class Movie_Type
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IQueryable<Movie> Movies { get; set; }
}
或
public partial class Movie_Type
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<Movie> Movies { get; set; }
}
编辑: -
@Tomas Petricek。
感谢您的回复。在我的情况下,我使用该数据库第一种方法,然后我用的DbContext
模板映射我的表,它可以自动创建的ICollection
对于所有的导航属性,所以我的问题是: -
1.这是否意味着它并不总是使用ICollection的最佳选择。我应该改变自动生成的类以最适合我的情况。
2.其次我可以通过.INCLUDE等定义为
-@Tomas Petricek.thanks for your reply. in my case i am using the database first approach and then i use DbContext
template to map my tables, which automatically created ICollection
for all the navigation properties, So my questions are:-1. Does this mean that it is not always the best choice to use Icollection. And i should change the automatically generated classes to best fit my case.2. Secondly i can manage to choose between lazy or Eager loading by defining .include such as
var courses = db.Courses.Include(c => c.Department);
不管我使用来定义导航属性什么。所以,我无法理解乌拉圭回合点。
3.我没有以往任何时候都发现任何实例或使用 IQuerable
来定义导航属性教程,,所以可能是什么原因呢?
BR
Regardless of what i am using to define the navigation properties. So i can not understand ur point.3. i did not ever find any examples or tutorials that use IQuerable
to define the navigation properties ,, so what might be the reason?BR
推荐答案
您不能使用类型的导航属性的IQueryable&LT; T&GT;
。您必须使用的ICollection&LT; T&GT;
或者一些集合类型,它实现的ICollection&LT; T&GT;
- 像列表&LT; T&GT;
。 (的IQueryable&LT; T&GT;
不执行的ICollection&LT; T&GT;
)
You cannot use a navigation property of type IQueryable<T>
. You must use ICollection<T>
or some collection type which implements ICollection<T>
- like List<T>
. (IQueryable<T>
does not implement ICollection<T>
.)
的导航属性只是一个对象或内存中的对象的集合或者是空
或集合为空。
The navigation property is simply an object or a collection of objects in memory or it is null
or the collection is empty.
这是从来没有从数据库加载时加载其中包含来自数据库的导航属性的父对象。
It is never loaded from the database when you load the parent object which contains the navigation property from the database.
您要么明确地说,你要与这预先加载父一起加载导航属性:
You either have to explicitely say that you want to load the navigation property together with the parent which is eager loading:
var movieTypes = context.Movie_Types.Include(m => m.Movies).ToList();
// no option to filter or sort the movies collection here.
// It will always load the full collection into memory
或者,它会被延迟加载加载(这是默认启用的,如果你的导航属性为虚拟
)
Or it will be loaded by lazy loading (which is enabled by default if your navigation property is virtual
):
var movieTypes = context.Movie_Types.ToList();
foreach (var mt in movieTypes)
{
// one new database query as soon as you access properties of mt.Movies
foreach (var m in mt.Movies)
{
Console.WriteLine(m.Title);
}
}
最后一个选项是的明确载入其中最接近你的意图我猜:
The last option is explicit loading which comes closest to your intention I guess:
var movieTypes = context.Movie_Types.ToList();
foreach (var mt in movieTypes)
{
IQueryable<Movie> mq = context.Entry(mt).Collection(m => m.Movies).Query();
// You can use this IQueryable now to apply more filters
// to the collection or sorting, for example:
mq.Where(m => m.Title.StartWith("A")) // filter by title
.OrderBy(m => m.PublishDate) // sort by date
.Take(10) // take only the first ten of result
.Load(); // populate now the nav. property
// again this was a database query
foreach (var m in mt.Movies) // contains only the filtered movies now
{
Console.WriteLine(m.Title);
}
}
这篇关于清单VS VS的IEnumerable IQueryable的定义导航属性时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!