简体   繁体   中英

Issue with lazy loading Framework Entity

I am using Entity with lazy loading and I have to following problem, I'm running a query that doesn't include related properties but when I try to serialize the object, serializer is trying to load the related properties and since my context is disposed I'm getting an exception.

What I want to do is set all related properties to null when they are not loaded (some times I might include them in my query and I'd like them to be serialized in that case). Is there a way I can do that?

Try following code. I use Json.Net for serialization, and set LazyLoadingEnabled = false.

using (var dbContext = new ApplicationDbContext())
{
    dbContext.Configuration.LazyLoadingEnabled = false;

    var list = dbContext.Videos.Take(5).ToList();

    var output = JsonConvert.SerializeObject(list);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM