2017-04-10 20 views
0

エンティティとコードを使用して外部キーの関係を追加しようとしています。私は以下の簡単な設定をしています。エンティティが外部キーを作成する

public class ChildClass 
{ 
    public int SId { get; set; } 
    [ForeignKey("SId")] 
    public ParentClass Parent { get; set; } 
} 

public class ParentClass 
{  
    [Key] 
    public int SId { get; set; }  
    public ChildClass Child { get; set; } 
} 

マイグレーションを試行して追加すると、次のエラーが表示されます。

Unable to determine the principal end of an association between the types 'ChildClass' and 'ParentClass'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 
+0

' "ParentClass" を入れてみてくださいは、代わりに' ' "のSId" の' –

+0

は注釈がdeclarartionを超えることになっていませんか? –

答えて

0

あなたのChildClassであなたのSIdプロパティがPKだけでなく、FKになりたい場合は、この試してみてください:作成され

public class ChildClass 
    { 
     [Key, ForeignKey("Parent")] 
     public int SId { get; set; } 

     public ParentClass Parent { get; set; } 
    } 

    public class ParentClass 
    { 
     [Key] 
     public int SId { get; set; } 

     public ChildClass Child { get; set; } 
    } 

テーブル:

親を:

enter image description here

子供:

enter image description here

関連する問題