【问题标题】:JsonResult disable Lazy loadingJsonResult 禁用延迟加载
【发布时间】:2014-01-13 10:31:07
【问题描述】:

我在我的 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 尝试这样做的方法?

【问题讨论】:

  • service.GetAll("MemberOf"); 返回什么?

标签: c# entity-framework lazy-loading


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多