Database connection errors on EF

前端 未结 2 390
别那么骄傲
别那么骄傲 2021-01-24 09:09

I am very new to entity framework and I am having a problem with a web api based site (connected to mssql) that I am writing. I keep getting seemingly random errors (mostly seem

2条回答
  •  悲哀的现实
    2021-01-24 09:41

    Since I do not know how your page uses the methods UsefulLinksController and in which order, I would say UsefulLinkExists is perhaps the culprit due to lazy loading

    Lazy loading means delaying the loading of related data until you specifically request it

    Which would explain why your "reader" remains "open".

    Try:

    return db.UsefulLinks.ToList().Count(e => e.ID == id) > 0;
    

    In any case, you can disable lazy loading by default in the context constructor as such noted here:

    public MyEntitiesContext() : base("name=MyEntitiesContext", "MyEntitiesContext")
    {
        this.ContextOptions.LazyLoadingEnabled = false;
        OnContextCreated();
    }
    

    As far as I know, it applies to EF4 and up.

提交回复
热议问题