I have three tables: PropertyOwner, Property and Address.
A PropertyOwner has lots of Properties.
A PropertyOwner has an Address.
Each Property also has an Address.
Here is the code first for Entity Framework 4.4:
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; }
When I try to create this database, I get the following error:
The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.PropertyOwner_dbo.Address_AddressId ]
Does the database think that the Property's Address and the PropertyOwner's Address are the same record?
How can I spell it out in the attributes that the Property needs to have it's own address and the PropertyOwner also needs to have its own Address?
Thanks.