Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid cyclical references in EF4.4 design using attributes?
    text
    copied!<p>I have three tables: <strong>PropertyOwner</strong>, <strong>Property</strong> and <strong>Address</strong>.</p> <p>A <strong>PropertyOwner</strong> has lots of <strong>Properties</strong>.</p> <p>A <strong>PropertyOwner</strong> has an <strong>Address</strong>.</p> <p>Each <strong>Property</strong> also has an <strong>Address</strong>.</p> <p>Here is the code first for Entity Framework 4.4:</p> <pre><code>Table: PropertyOwner [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] [Required] public Guid PropertyOwnerId { get; set; } [Required] [ForeignKey("Address")] public Guid AddressId { get; set; } public virtual Address Address { get; set; } Table: Property [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] [Required] public Guid PropertyId { get; set; } [Required] [ForeignKey("PropertyOwner")] public Guid PropertyOwnerId { get; set; } public virtual PropertyOwner PropertyOwner { get; set; } [Required] [ForeignKey("Address")] public Guid AddressId { get; set; } public virtual Address Address { get; set; } Table: Address [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] [Required] public Guid AddressId { get; set; } [Required] [StringLength(64)] public string AddressLine1 { get; set; } </code></pre> <p>When I try to create this database, I get the following error:</p> <pre><code>The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.PropertyOwner_dbo.Address_AddressId ] </code></pre> <p>Does the database think that the <strong>Property</strong>'s <strong>Address</strong> and the <strong>PropertyOwner</strong>'s <strong>Address</strong> are the same record?</p> <p>How can I spell it out in the attributes that the <strong>Property</strong> needs to have it's own address and the <strong>PropertyOwner</strong> also needs to have its own <strong>Address</strong>?</p> <p>Thanks.</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