Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are getting is the default behavior of EF Code First in terms of mapping a 1 to Many association to the database. For example you have a <code>ICollection&lt;File&gt;</code> on Person class, as a result, EF will create a FK on Files table (PersonId) and map it to Id PK on Persons table. <br><br> Now, my guess is that you like to have a <em>many to many</em> relationship between File and Person, so that each file can relates to many Persons and each Person can have many files (same story for Event object as well). One way to achieve this is to put navigation properties on File class pointing to Event and Person classes. So, your model should be changed to this:</p> <pre><code>public class File { public int Id { get; set; } public string Path { get; set; } public virtual ICollection&lt;Event&gt; Events { get; set; } public virtual ICollection&lt;Person&gt; Persons { get; set; } } public class Event { public int Id { get; set; } public string EventName { get; set; } public virtual ICollection&lt;File&gt; Files {get;set;} } public class Person { public int Id { get; set; } public string Name { get; set; } public virtual ICollection&lt;File&gt; Files { get; set; } } public class MyContext : DbContext { public DbSet&lt;Person&gt; Persons { get; set; } public DbSet&lt;Event&gt; Events { get; set; } public DbSet&lt;File&gt; Files { get; set; } } </code></pre> <p>As a result, EF will create link tables (Events_Files and Files_Persons) to map these many to many associations to the database.</p> <p><strong>Update:</strong> <br> When using POCOs with EF, if you mark your navigation properties as <em>virtual</em> you will opt-in to some of the additional EF supports like <em>Lazy Loading</em> and <em>Relationship Fixup</em>. So in general have a virtual keyword in navigation properties considered to be a good practice.</p>
    singulars
    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.
    3. 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