简体   繁体   中英

Loading related data in EF Core

I have a question about Entity Framework Core. Let's say there is a DbContext provides work with 2 entities:
Person { int Id; int PassportId; Passport Passport }
Passport { int Id; int PersonId; Person Person}

As a result of query

Person person = context.Person.Include(p => p.Passport).FirstOrDefault();

I get Person object (person) which refers to Passport's object. However, there is also reference to person from Passport object.

How? Why does implicit binding of a Passport object to a Person object provide? I seem it's due to the navigation properties and that they work both ways. Share information, please.

You're getting a circular reference, did you enabled Lazy-Loading? If the reference it's causing an exception you can use this to avoid it.

services.AddMvc()
.AddJsonOptions(options => {
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

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