1

EF-database-first を使用して MVC4 Web アプリケーションを作成しました。テーブルには複合キー [ID, Name, EffDate] があり、データベースに定義された外部キーはありません: たとえば、Department 部分クラス:

[MetadataType(typeof(DepartmentMetadata))]
public partial class Department
{
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public System.DateTime EffDate { get; set; }
    public string Status { get; set; }
    public string RevenueAccount { get; set; }
}

部門メタデータ クラス:

public class DepartmentMetadata
{
    [Required]
    public int DeptID { get; set; }

    [Required]
    [Display(Name = "Department Name")]
    public string DeptName { get; set; }

    [Required]
    [Display(Name = "Effective Date")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", NullDisplayText = "--", ConvertEmptyStringToNull = true)]
    public System.DateTime EffDate { get; set; }

    [Required]
    public string Status { get; set; }

    [Display(Name = "Revenue Account")]
    [StringLength(10)]
    public string RevenueAccount { get; set; }
}

Department テーブルを参照する Allocation テーブル。複合キー [DeptID、ProjectID、BillableUnitID、EffDate] もあります。できれば、DeptID フィールドを外部キーとして宣言しますが、データベースを制御することはできません。さらに重要なことに、T-SQL では外部キーを複合キーの一部にすることはできないと思います。

[MetadataType(typeof(AllocationMetadata))]
public partial class Allocation
{
    public int DeptID { get; set; }
    public int ProjectID { get; set; }
    public int BillableUnitID { get; set; }
    public System.DateTime EffDate { get; set; }
    public string Status { get; set; }
    public decimal Allocation1 { get; set; }
}

これは機能しますが、DeptID 番号の列を取得します。欲しいのは部署名の列です。

以前の質問で仮想ナビゲーション プロパティに誘導されたので、それらを追加しました。

[MetadataType(typeof(AllocationMetadata))]
public partial class Allocation
{
    [ForeignKey("Department")]
    public int DeptID { get; set; }
    public int ProjectID { get; set; }
    public int BillableUnitID { get; set; }
    public System.DateTime EffDate { get; set; }
    public string Status { get; set; }
    public decimal Allocation1 { get; set; }

    public virtual Department Department { get; set; } /* navigation property */
}

Index の AllocationController のコードは次のとおりです。 public ActionResult Index() { return View(db.Allocation.Include(a => a.Department).ToList()); }

割り当てインデックス ビューへのリンクをクリックすると、次のエラー メッセージが表示されます (デバッグを停止した後)。

「/」アプリケーションでサーバー エラーが発生しました。

指定されたインクルード パスが無効です。EntityType 'KC_BillableUnit_TESTModel.Allocation' は、'Department' という名前のナビゲーション プロパティを宣言していません。

スタック トレース [InvalidOperationException: 指定されたインクルード パスが無効です。EntityType 'KC_BillableUnit_TESTModel.Allocation' は、'Department' という名前のナビゲーション プロパティを宣言していませ

。 Objects.Internal.ObjectFullSpanRewriter..ctor(DbCommandTree tree, DbExpression toRewrite, Span span) +256 ....continues....

注釈のさまざまな組み合わせを試しましたが、すべて同じエラーになります。

割り当てリストに DeptID 番号の代わりに部門名を表示するにはどうすればよいですか?

4

1 に答える 1

0

もちろんできます!問題は、ナビゲーション プロパティを片側 (割り当て) だけで宣言したことだと思いますが、両側 (部門も) で宣言する必要があります。

次の手順で問題を解決する必要があります。

[MetadataType(typeof(DepartmentMetadata))]
public partial class Department
{
    public Department()
    {
        this.Allocations = new HashSet<Allocation>();
    }

    // properties ...

    public virtual ICollection<Allocation> Allocations { get; set; }
}
于 2013-07-26T02:22:23.027 に答える