0

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.

BrainProxy
  • 107
  • 7
  • I think you don't need to explicitly specify foreign keys in classes `Property` and `PropertyOwner`. Proper keys will be generated in the DB by EF, based only on `public virtual Address Address` property. Try that and see if that gets rid of the problem. – Floremin Mar 20 '13 at 18:06
  • Thanks, but I removed all mention of foreign keys and it makes no difference. – BrainProxy Apr 06 '13 at 11:33
  • This guy had a similar situation, take a look at this: http://stackoverflow.com/questions/7367339/net-mvc-cyclical-reference-issue-with-entity – Floremin Apr 08 '13 at 00:10

0 Answers0