我有一个代表用户填写表单的POCO。表单上有大约10个不同的地址,因此我将它们分成单独的地址POCO。问题是如何建立关系。
我希望能做什么:
Public Class formtofillout
Public Property form_id As Integer
<ForeignKey("address_1")> _
Public Property address_1_id As Integer
<ForeignKey("address_2")> _
Public Property address_2_id As Integer
<ForeignKey("address_3")> _
Public Property address_3_id As Integer
<ForeignKey("address_4")> _
Public Property address_4_id As Integer
<ForeignKey("address_5")> _
Public Property address_5_id As Integer
<ForeignKey("address_6")> _
Public Property address_6_id As Integer
<ForeignKey("address_7")> _
Public Property address_7_id As Integer
<ForeignKey("address_8")> _
Public Property address_8_id As Integer
<ForeignKey("address_9")> _
Public Property address_9_id As Integer
<ForeignKey("address_10")> _
Public Property address_10_id As Integer
Public Overridable Property address_1 As address
Public Overridable Property address_2 As address
Public Overridable Property address_3 As address
Public Overridable Property address_4 As address
Public Overridable Property address_5 As address
Public Overridable Property address_6 As address
Public Overridable Property address_7 As address
Public Overridable Property address_8 As address
Public Overridable Property address_9 As address
Public Overridable Property address_10 As address
End Class
并不确定如何处理地址POCO来代表这种关系的另一端。但是......这给了我一系列的错误:
这一个:
Introducing FOREIGN KEY constraint 'FK_dbo.formtofillout_dbo.address_1_id' on table
'formtofillout' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION
or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
我想其他几个。
为了使其工作,我没有在'formtofillout'POCO中创建特定的id列。相反,我只是创建了导航属性:
Public Class formtofillout
Public Property form_id As Integer
Public Overridable Property address_1 As address
Public Overridable Property address_2 As address
Public Overridable Property address_3 As address
Public Overridable Property address_4 As address
Public Overridable Property address_5 As address
Public Overridable Property address_6 As address
Public Overridable Property address_7 As address
Public Overridable Property address_8 As address
Public Overridable Property address_9 As address
Public Overridable Property address_10 As address
End Class
然后在OnModelBuilding事件中,我为每个地址添加了以下内容:
modelBuilder.Entity(Of formtofillout).HasOptional(Function(n) n.address_1).WithRequired().WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasOptional(Of address)(Function(n) n.address_1).WithMany().Map(Function(m) m.MapKey("address_1_id"))
这很有效,当我执行迁移时,表是使用address_1_id,address_2_id列等创建的。但是这些ID列在数据库中,但不在POCO中。如何设置它以获取POCO和表中的外键列?
答案 0 :(得分:0)
您需要将cascadeOnDelete(false)添加到外键定义中。 将多个引用返回到一个表或a-> b-&gt; c-&gt;类型方案EF不能处理: 例如
entity.HasRequired(p=>p.SomeNavProp)
.WithMany()
// .HasForeignKey(p=>p.RefId)
.WillCascadeOnDelete(false);
不要忘记Virtual Nav属性定义。在你的代码提取中没有看到它
答案 1 :(得分:0)
好的,想通了。这是我的班级:
Public Class formtofillout
Public Property form_id As Integer
Public Property address_1_id As Integer
Public Property address_2_id As Integer
Public Property address_3_id As Integer
Public Property address_4_id As Integer
Public Property address_5_id As Integer
Public Property address_6_id As Integer
Public Property address_7_id As Integer
Public Property address_8_id As Integer
Public Property address_9_id As Integer
Public Property address_10_id As Integer
Public Overridable Property address_1 As address
Public Overridable Property address_2 As address
Public Overridable Property address_3 As address
Public Overridable Property address_4 As address
Public Overridable Property address_5 As address
Public Overridable Property address_6 As address
Public Overridable Property address_7 As address
Public Overridable Property address_8 As address
Public Overridable Property address_9 As address
Public Overridable Property address_10 As address
End Class
这是DbContext中的OnModelCreating,我这样做了:
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_1).WithMany().HasForeignKey(Function(a) a.address_1_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_2).WithMany().HasForeignKey(Function(a) a.address_2_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_3).WithMany().HasForeignKey(Function(a) a.address_3_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_4).WithMany().HasForeignKey(Function(a) a.address_4_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_5).WithMany().HasForeignKey(Function(a) a.address_5_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_6).WithMany().HasForeignKey(Function(a) a.address_6_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_7).WithMany().HasForeignKey(Function(a) a.address_7_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_8).WithMany().HasForeignKey(Function(a) a.address_8_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_9).WithMany().HasForeignKey(Function(a) a.address_9_id).WillCascadeOnDelete(False)
modelBuilder.Entity(Of formtofillout).HasRequired(Function(n) n.address_10).WithMany().HasForeignKey(Function(a) a.address_10_id).WillCascadeOnDelete(False)