2012-06-22 7 views
29

私はOracleでEntity Framework 4.3を最初に使用しています。私は次のエラーを取得しています:カスタムの列名を持つ外部キーのマッピング

System.InvalidOperationException : The ForeignKeyAttribute on property 'WidgetSequence' on type 'WidgetDistributor.WidgetEntity' is not valid. The foreign key name 'WIDGETSEQUENCE_ID' was not found on the dependent type 'WidgetDistributor.WidgetEntity'. The Name value should be a comma separated list of foreign key property names.

私のエンティティは、このようなものです:

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [ForeignKey("WIDGETSEQUENCE_ID")] 
    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

私のコードが正しいようです。私は何を間違えたのですか?

答えて

28

ForeignKey attibuteは、クラス内のプロパティ名を引数として想定していますが、カラム名を指定しています。流暢なマッピングを使用してください。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 

    modelBuilder.Entity<WidgetEntity>() 
    .HasRequired(w => w.Sequence) 
    .WithMany() 
    .Map(m => m.MapKey("WIDGETSEQUENCE_ID")); 
} 
+0

ありがとう:

は、私はこのような外部キーをマップするためのForeignKeyアノテーションを使用することができますよ。この問題はどうですか?どうぞご覧ください。事前に感謝... http://stackoverflow.com/questions/29333787/how-to-create-lookup-table-and-define-relationships –

47

あなたは流暢な構文を使用したくない場合は、データアノテーションを使用して参照を実装する他の3つの方法があります(彼らは読みやすいようで、ちょうど財産彼らの上に書かれているよう個人的に私は、データの注釈を好みます)に影響される:

1.1) 関連性のある使用のForeignKey() - バージョン1

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [Column("WIDGETSEQUENCE_ID")] 
    public int WidgetSequenceId { get; set; } 

    [ForeignKey("WidgetSequenceId")] //Has to be a property name, not table column name 
    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

1.2) 関連性のある使用のForeignKey() - バージョン2

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [ForeignKey("Sequence")] //Has to be a property name, not table column name 
    [Column("WIDGETSEQUENCE_ID")] 
    public int WidgetSequenceId { get; set; } 

    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

2) InversePropertyAttributeを使用することもできます。

+0

ありがとう!ドキュメンテーションと他のいくつかの答えは、逆プロパティが本当にすべきであるところで、外部キー属性が使用できることを示唆しています。 – Carl

+0

@Carl、あなたが見つけた提案は正しい方法ではありません。 FK属性は、逆のプロパティではなく、列の上に表示されます。 – vapcguy

+0

@RichardPierre返信いただきありがとうございます。しかし、InversePropertyを持つId列以外の列は参照できません。この問題はどうですか?何が問題なの? http://stackoverflow.com/questions/29327772/how-to-define-matching-column-in-inverseproperty?noredirect=1#comment46851150_29327772 –

0

Usersという表があり、UserIDという主キーがあります。

ディレクトリと呼ばれる別のテーブルがあり、それにはUsersテーブルという外部キーとして定義されたUserIDという列があります。返信用

[ForeignKey("xyzzy")] 
public int? UserID { get; set; } // This is a column in the table 
public virtual User xyzzy { get; set; } // This is my instance of User 
関連する問題