Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your foreign column names in the database do not match the default convention for FK names which is <code>NavigationPropertyName_PrimaryKeyNameinTargetClass</code>. Because your navigation properties are called <code>EnteredBy</code> and <code>ModifiedBy</code> and the primary key property is called <code>OperatorCode</code> EF expects - according to the mentioned convention - <code>EnteredBy_OperatorCode</code> and <code>ModifiedBy_OperatorCode</code> as foreign key columns. But those do not exist in the database which is the reason for your exception. Instead your FK columns are <code>EnteredBy</code> and <code>ModifiedBy</code>.</p> <p>So, to fix the problem you must override the convention.</p> <p>If you don't have FK properties in your model use Fluent API:</p> <pre><code>modelBuilder.Entity&lt;Operator&gt;() .HasMany(o =&gt; o.ClientsEnteredBy) .WithRequired(c =&gt; c.EnteredBy) // or HasOptional .Map(m =&gt; m.MapKey("EnteredBy")); // mapping for the FK column name modelBuilder.Entity&lt;Operator&gt;() .HasMany(o =&gt; o.ClientsUpdatedBy) .WithRequired(c =&gt; c.ModifiedBy) // or HasOptional .Map(m =&gt; m.MapKey("ModifiedBy")); // mapping for the FK column name </code></pre> <p>(With this mapping you can remove the <code>InverseProperty</code> attribute.)</p> <p>An alternative approach is to expose the FKs as properties in the model. Rename the navigation properties and use their names for the FK properties. The mapping is then possible with data annotations.</p> <p>In <code>Client</code> class:</p> <pre><code>[ForeignKey("EnteredByOperator")] public string EnteredBy { get; set; } [InverseProperty("ClientsEnteredBy")] public virtual Operator EnteredByOperator { get; set; } [ForeignKey("ModifiedByOperator")] public string ModifiedBy { get; set; } [InverseProperty("ClientsUpdatedBy")] public virtual Operator ModifiedByOperator { get; set; } </code></pre> <p>And remove the <code>InverseProperty</code> attributes in the <code>Operator</code> class.</p> <p>Instead of the data annotations you can also use Fluent API:</p> <pre><code>modelBuilder.Entity&lt;Operator&gt;() .HasMany(o =&gt; o.ClientsEnteredBy) .WithRequired(c =&gt; c.EnteredByOperator) // or HasOptional .HasForeignKey(c =&gt; c.EnteredBy); modelBuilder.Entity&lt;Operator&gt;() .HasMany(o =&gt; o.ClientsUpdatedBy) .WithRequired(c =&gt; c.ModifiedByOperator) // or HasOptional .HasForeignKey(c =&gt; c.ModifiedBy); </code></pre> <p>If both relationships are required you will need to disable cascading delete for at least one of the relationships (append <code>.WillCascadeOnDelete(false)</code> at the end of one of the mappings), otherwise SQL Server will throw an error that multiple cascading delete paths between the tables are not allowed.</p> <p>I would suggest to use the "alternative approach" (expose foreign keys as properties) because it is easier to work with in most cases.</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