实体框架和MVC3包含方法
问题描述:
LINQ“include”缺失。看完这篇文章后: http://romiller.com/2010/07/14/ef-ctp4-tips-tricks-include-with-lambda/实体框架和MVC3包含方法
我想用include。 这是我的课:
public class Service
{
#region Properties
/// <summary>
/// Gets or sets CatalogRootNodeId.
/// </summary>
public virtual int CatalogRootNodeId { get; set; }
/// <summary>
/// Gets or sets ServiceDomain.
/// </summary>
public virtual ICollection<ServiceDomain> ServiceDomain { get; set; }
#endregion
}
我想“包含”所有ServiceDomains但“包含”选项是不存在 ? 我正在使用MVC3和EF。 谢谢
答
您使用的是CTP5还是CTP4? 如果这样做,可以使用System.Data.Entity.DbExtensions.Include中的扩展方法。
public static IQueryable<T> Include<T>(this IQueryable<T> source, Expression<Func<T, object>> path)
var db = new MyDbContext();
var services = db.Services.Where(s => s.CatalogRootNodeId == 1).Include(s => s.ServiceDomain);
答
您需要拨打Include()
ObjectSet<Service>
从DataContext
。