1

記事用に次のモデルがあります。

public class Article
{
    [Key]
    public int ArticleId { get; set; }

    [Required(ErrorMessage = "Title is required."), MaxLength(80)]
    public string Title { get; set; }

    [Required(ErrorMessage = "Body is required.")]
    public string Body { get; set; }

    public DateTime Time { get; set; }

    public int AuthorId { get; set; }

    // Navigation properties
    public virtual UserProfile Author { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

これUserProfileは、MVC4 標準プロジェクトのデフォルトの拡張バージョンです。

今、私のスキャフォールディングされたコントローラー/ビューでは、Author.

私のデータベース (MySQL) には、namedAuthor_UserIdの typeのフィールドが含まれていますint

なにが問題ですか?

また、ナビゲーション プロパティAuthorId

4

1 に答える 1

0

どのテーブルでも、特定のテーブルの2つの同じ外部キーを使用することは一般的ではありません。このようなものは、データの冗長性として解釈されます。冗長性なしで問題を解決するために、私はあなたに以下のモデルを提案します:

public class Article
{
    [Key]
    public int ArticleId { get; set; }
    [Required(ErrorMessage = "Title is required."), MaxLength(80)]
    public string Title { get; set; }

    [Required(ErrorMessage = "Body is required.")]
    public string Body { get; set; }

    public DateTime Time { get; set; }

    public int AuthorId { get; set; }//determine name of foreign key here of type primary key of navigatable table

    // Navigation properties
    [ForeignKey("AuthorId")]
    public virtual UserProfile Author { get; set; }//column with name 'AuthorId'
    public virtual ICollection<Tag> Tags { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

上記の解決策は、ナビゲーションプロパティの外部キーに自己命名するために使用されます。お役に立てれば幸いです。

于 2012-11-26T11:52:58.720 に答える