Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 4.1 Code First - How to create two different relationships between two tables
    text
    copied!<p>I need to create a relationship where I have a user table that links to an address table. The problem is that I need the address table to also store historic addresses. It is also possible that a user might not have an address at all. </p> <pre><code>public class user { public virtual int ID { get; set; } ... public virtual int? AddressId { get; set; } [ForeignKey("AddressId")] public virtual Address CurrentAddress { get; set; } public virtual ICollection&lt;Address&gt; HistoricAddresses { get; set; } } public class Address { public virtual int ID { get; set; } ... } </code></pre> <p>I tried various ways to get this to work and got various errors like putting another table between User and Address:</p> <pre><code>public class HistoricAddress { public virtual int ID { get; set; } public Address HistoricAddress { get; set; } } public class user { public virtual int ID { get; set; } public virtual Address CurrentAddress { get; set; } public virtual ICollection&lt;HistricAddress&gt; HistoricAddresses { get; set; } ... } </code></pre> <p>and various other ways, but this also throws up errors. There must be a proper way of doing this. The last error I got was:</p> <p>"System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.InvalidOperationException: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'."</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