How can I prevent EF7 from eagerly fixing navigation properties?

I have a problem with EF7 in a web application with which I could use some help. I am currently using EF7 RC1.

Here are some models that illustrate my problem.

Contact

public class Contact
{
    public Guid Id { get; set; }

    public string Desc { get; set; }
    public ContactType ContactType { get; set; }
}

ContactType

public class ContactType
{
    public Guid Id { get; set; }
    public string Desc { get; set; }

    public ICollection<Contact> Contacts { get; set; }
}

These models are linked through the Fluent API as follows:

modelBuilder.Entity<Contact>(entity => {
     // abridged for clarity

     entity
        .HasOne(c => c.ContactType)
        .WithMany(ct => ct.Contacts)
        .IsRequired();                
});

My need is to be able to retrieve a collection of Contact objects from a database with the ContactType property loaded. EF makes this pretty simple:

using(var context = new MyDbContext()) {
    var contacts = await context
        .Contacts
        .Include(c => c.ContactTypes)
        .Where(/* some search criteria */)
        .ToListAsync();
}

, ContactType Contact ( - .Include() ) EF Contacts ContactType, , ContactTypes ContactTypes, . , , - JSON - .

, EF ( ) ContactType, "" . - EF? ? , , ?

, :

  • .AsNoTracking() EF (, , ContactType )
  • Json.NET ( , , ).
+3
2

EF ContactType.Contacts, , . AsNoTracking , ChangeTracker.

:

  • Json.NET ReferenceLoopHandling = ReferenceLoopHandling.Ignore, , , , ContactType
  • [JsonIgnore] ContactType.Contacts, . , , .
  • DTO, - Automapper ( Contacts)

, , .

+3

Entity Framework 7 Core

AsNoTracking()

IQueryable<ScheduleModel> q = _db.Schedules;
q = q.AsNoTracking();
q = q.Include(x => x.ElementItem);
q = q.Include(x => x.ScheduleHours);

.

0

Source: https://habr.com/ru/post/1670236/


All Articles