首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonResult禁用延迟加载

JsonResult禁用延迟加载
EN

Stack Overflow用户
提问于 2014-01-13 10:31:07
回答 1查看 176关注 0票数 1

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

代码语言:javascript
复制
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

        // ...
    }
}

但当我运行这段代码时:

代码语言:javascript
复制
// 
// 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 } };
    }
}

它试图通过延迟加载加载所有属性。我的用户类如下所示:

代码语言:javascript
复制
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尝试这样做的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-13 10:56:11

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

代码语言:javascript
复制
public SkipstoneContext()
    : base("DefaultConnection")
{
    base.Configuration.LazyLoadingEnabled = false;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21089046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档