JsonResult禁用延迟加载

时间:2014-01-13 10:31:08

标签: c# entity-framework lazy-loading

我在DbContext中禁用了延迟加载,如下所示:

public partial class SkipstoneContext : DbContext
{
    static SkipstoneContext()
    {
        Database.SetInitializer<SkipstoneContext>(null); // Exsting database, do nothing
    }

    public SkipstoneContext()
        : base("DefaultConnection")
    {
    }

    // ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.Configuration.LazyLoadingEnabled = false; // Disable Lazy Loading

        // ...
    }
}

但是当我运行这段代码时:

// 
// AJAX: /Users/Get

public JsonNetResult Get()
{
    try
    {
        using (var service = new UserService(this.Context, this.CompanyId))
        {
            var u = service.GetAll("MemberOf");

            return new JsonResult { Data = new { success = true, users = u } }; // Return our users
        }
    }
    catch (Exception ex)
    {
        return new JsonResult { Data = new { success = false, error = ex.Message } };
    }
}

它试图通过延迟加载加载所有属性。 我的用户类看起来像这样:

public partial class User : IdentityUser
{
    public string CompanyId { get; set; }
    public string CreatedById { get; set; }
    public string ModifiedById { get; set; }
    public System.DateTime DateCreated { get; set; }
    public Nullable<System.DateTime> DateModified { get; set; }
    public System.DateTime LastLoginDate { get; set; }
    public string Title { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string JobTitle { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public string Photo { get; set; }
    public string LinkedIn { get; set; }
    public string Twitter { get; set; }
    public string Facebook { get; set; }
    public string Google { get; set; }
    public string Bio { get; set; }
    public string CompanyName { get; set; }
    public string CredentialId { get; set; }
    public bool IsLockedOut { get; set; }
    public bool IsApproved { get; set; }
    public bool CanEditOwn { get; set; }
    public bool CanEdit { get; set; }
    public bool CanDownload { get; set; }
    public bool RequiresApproval { get; set; }
    public bool CanApprove { get; set; }
    public bool CanSync { get; set; }
    public bool AgreedTerms { get; set; }
    public bool Deleted { get; set; }

    public Company Company { get; set; }
    public User CreatedBy { get; set; }
    public User ModifiedBy { get; set; }
    public ICollection<Asset> Assets { get; set; }
    public ICollection<Category> Categories { get; set; }
    public ICollection<Collection> Collections { get; set; }
    public ICollection<Comment> Comments { get; set; }
    public ICollection<LocalIntegration> LocalIntegrations { get; set; }
    public ICollection<Page> Pages { get; set; }
    public ICollection<Rating> Ratings { get; set; }
    public ICollection<Theme> Themes { get; set; }
    public ICollection<Group> MemberOf { get; set; }
    public ICollection<Category> ForbiddenCategories { get; set; }
    public ICollection<Page> ForbiddenPages { get; set; }
}

有没有人知道如何阻止JsonResult尝试这样做?

1 个答案:

答案 0 :(得分:1)

您必须将此代码base.Configuration.LazyLoadingEnabled = false;移动到SkipstoneContext构造函数中,以全局禁用上下文中的延迟加载。像这样:

public SkipstoneContext()
    : base("DefaultConnection")
{
    base.Configuration.LazyLoadingEnabled = false;
}
相关问题