Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are worried about lazy loading, then you could make the navigation property not virtual so that it will never kick in lazy loading.</p> <p>Or you could make the navigation property private and then access the contents in a controlled manner. Once you make the navigation property private you will need to use one of the tricks for mapping private properties using the Code First API. For simplicity I'm using the one that embeds the EntityConfiguration inside the entity:</p> <pre><code>public class Attachment { public Guid Id { get; set; } private ICollection&lt;User&gt; Users { get; set; } public class AttachmentConfiguration : EntityTypeConfiguration&lt;Attachment&gt; { public AttachmentConfiguration() { HasMany(e =&gt; e.Users) .WithMany(e =&gt; e.Attachements) .Map(m =&gt; { m.MapLeftKey("UserId"); m.MapRightKey("AttachmentId"); m.ToTable("User_Attachments"); }); } } } </code></pre> <p>Add this configuration inside OnModelCreating:</p> <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new Attachment.AttachmentConfiguration()); } </code></pre> <p>You can then access Users from inside Attachment in any way you want or expose the users from methods, etc.</p> <p>You could also use the DbContext change tracker API to access them from anywhere you have a context. For example:</p> <pre><code>var users = context.Entry(attachment).Collection&lt;User&gt;("Users").CurrentValue; </code></pre> <p>If you really want no navigation property at all, then you could drop down to ObjectContext and get the RelatedEnd for the collection, which will be an EntityCollection. This is pretty tricky code for a number of reasons, including needing to know the conceptual model names for the association and end names, which you could find by dumping the EDMX. I'd avoid this approach if at all possible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload