包含扩展方法的类库参考

时间:2016-07-09 09:37:56

标签: c# asp.net asp.net-mvc entity-framework

我正在阅读教程http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

在那里我发现了一些像

这样的代码
viewModel.Instructors = db.Instructors
    .Include(i => i.OfficeAssignment)

我想了解.Include方法。参考在哪里? (我期待像类库参考一样)

2 个答案:

答案 0 :(得分:4)

为了更好地理解它是如何工作的,让我们来看看这些示例实体类:

 public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
    public virtual ICollection<Teacher> Teachers { get; set; }
}

public class StudentAddress 
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }

    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int Zipcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }

    public virtual ICollection<StudentAddressDetail> StudentAddressDetails { get; set; }

    public virtual Student Student { get; set; }
}

急切加载

当您希望从一开始就可以使用单个查询命令将主实体(或实体集合)与其相关实体一起加载时,Eager Loading功能非常有用。为了使用它,您需要以下列方式使用Include()方法:

using (var ctx = new SchoolDBEntities())
{
    // Loads the students AND all related StudentAddress using Eager Loading
    IList<Student> sList = ctx.Students.Include(s => s.StudentAddress).ToList<Student>();
    Student s = sList[0];
}

重要如果找不到Include()方法,请检查您是否添加了System.Data.Entity命名空间。

您还可以使用Eager Loading来加载嵌套的多级属性。例如,我们可以通过以下方式加载每个StudentAddress项的StudentAddressDetails集合属性(参见上面的StudentAddress类定义):

using (var ctx = new SchoolDBEntities())
{
    // Loads the students AND all related StudentAddress AND all related StudentAddressDetails using Eager Loading
    IList<Student> sList = ctx.Students.Include(s => s.StudentAddress.StudentAddressDetails).ToList<Student>();
    Student s = sList[0];
}

这已得到很好的解释here。上面的答案是这篇文章的摘录:

答案 1 :(得分:2)

在您的文件中添加'use strict'; var SignalRWPFactory = function ($rootScope, DataService) { var _this = this; _this.rootScope = $rootScope; _this.dataService = DataService; _this.init = function (myHub, fn) { var _this = this; $.connection.hub.url = "http://localhost:5207/signalr";//i think in the startup we had specified this _this.create(myHub, fn); _this.update(myHub, fn); _this.deleteItem(myHub, fn); $.connection.hub.start(); }, _this.create = function (myHub, fn) { var _this = this; myHub.client.language = function (response) { if (response != "") { $rootScope.$apply(function () { fn(response, 'create'); }); } }; }, _this.update = function (myHub,fn) { var _this = this; myHub.client.languageUp = function (response) { if (response != "") { $rootScope.$apply(function () { fn(response, 'update'); }); } }; }, _this.deleteItem = function (myHub, fn) { var _this = this; myHub.client.languageDel = function (response) { if (response != "") { $rootScope.$apply(function () { fn(response, 'deleteItem'); }); } }; } return { init: _this.init, create: _this.create, update: _this.update, deleteItem: _this.deleteItem }; }; SignalRWPFactory.$inject = ['$rootScope', 'DataService']; webAdmin.factory('SignalRWPFactory', SignalRWPFactory); **> here is the usage from my end > > in the controller use like this** // Declare a proxy to reference the hub. var myHub = $.connection.languageHub; _this.signalRWPFactory.init(myHub, signalRCallback); function signalRCallback(data, callType) { _this.data = _this.dataService.handleSignalRData(_this.data, data, callType); //it will delete the data from data array // $scope.$apply(); } _this.data is the array object you can handle create separate service. ** > here is the data handler method as well ** handleSignalRData: function (dataArr, data, type) { var _this = this; //sometimes its coming as object if (Object.prototype.toString.call(data) === '[object Array]') { data = data[0]; } switch (type) { case 'create': dataArr.push(data); break; case 'update': dataArr = _this.update(dataArr, data); //it will update the row break; case 'deleteItem': dataArr = _this.deleteByAttr(dataArr, "id", data.id); //it will update the row break; default: } return dataArr; }