Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know how to make it work with data annotations but with Fluent API you can apply and experiment with the trick from here: <a href="http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/" rel="nofollow">http://blog.cincura.net/232731-mapping-private-protected-properties-in-entity-framework-4-x-code-first/</a></p> <p>For your model it would look like the following:</p> <pre><code>public class User { public virtual long Id{get; set;} public virtual ICollection&lt;Tag&gt; Tags { get; protected set; } } public class Tag { public virtual long Id{get; set;} internal protected virtual ICollection&lt;User&gt; Users { get; set; } public class PropertyAccessors { public static readonly Expression&lt;Func&lt;Tag, ICollection&lt;User&gt;&gt;&gt; Users = t =&gt; t.Users; } } </code></pre> <p>Mapping in FluentAPI:</p> <pre><code>modelBuilder.Entity&lt;User&gt;() .HasMany(u =&gt; u.Tags) .WithMany(Tag.PropertyAccessors.Users); </code></pre> <p>This works and creates the expected many-to-many relationship.</p> <p>But I am not sure if you can do anything useful with that navigation property. The fact that you have the property <code>protected</code> and <code>virtual</code> lets me guess that you basically want lazy loading support inside of the entity class or derived classes.</p> <p>The problem is that apparently (according to my tests at least) lazy loading doesn't work for anything else than a <code>public</code> property. The loaded tag is a proxy but the navigation collection is always <code>null</code> (unless the property is <code>public</code>).</p> <p>Moreover, even eager and explicit loading don't work:</p> <p>Outside of the <code>Tag</code> class:</p> <pre><code>// Eager loading var tag = context.Tags.Include(Tag.PropertyAccessors.Users).First(); // Explicit loading var tag2 = context.Tags.First(); context.Entry(tag2).Collection(Tag.PropertyAccessors.Users).Load(); </code></pre> <p>Or inside of the <code>Tag</code> class (some method in <code>Tag</code> which gets the context passed):</p> <pre><code>public DoSomething(MyContext context) { // Eager loading var tag = context.Tags.Include(t =&gt; t.Users).First(); // Explicit loading context.Entry(this).Collection(t =&gt; t.Users).Load(); } </code></pre> <p>In all cases I get an exception that the property <code>Users</code> on entity <code>Tag</code> is not a valid navigation property. (The exception disappears as soon as I make the property <code>public</code>.)</p> <p>I don't know if adding/removing/updating relationships would work. (I doubt.)</p> <p>It looks that you can map a non-public navigation property with this approach to generate the expected database schema. But it seems that there is nothing useful you can do with it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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