0

ユーザーが入力するフォームを表す 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 とテーブルの外部キー列を取得するように設定するにはどうすればよいですか?

4

2 に答える 2

0

OK、これを理解しました。これが私のクラスです:

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)
于 2013-05-29T15:56:08.150 に答える
0

外部キー定義に cascadeOnDelete(false) を追加する必要があります。1 つのテーブルまたは a->b->c->a タイプのシナリオに複数の参照を戻す場合、EF はそれを処理できません。

   entity.HasRequired(p=>p.SomeNavProp)
              .WithMany()  
         //  .HasForeignKey(p=>p.RefId)
            .WillCascadeOnDelete(false);

Virtual Nav プロパティの定義を忘れないでください。あなたのコード抽出でそれを見ませんでした

于 2013-05-24T01:54:19.533 に答える