如何使用属性避免EF4.4设计中的循环引用?

时间:2013-03-20 17:44:57

标签: c# entity-framework ef-code-first

我有三个表: PropertyOwner 财产地址

PropertyOwner 有很多属性

PropertyOwner 有一个地址

每个属性也有地址

以下是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; }

当我尝试创建此数据库时,出现以下错误:

The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = FK_dbo.PropertyOwner_dbo.Address_AddressId ]

数据库是否认为 Property 地址 PropertyOwner 地址是相同的记录?

如何在 Property 需要拥有自己的地址的属性中拼写出来, PropertyOwner 也需要拥有自己的地址

感谢。

0 个答案:

没有答案