0
我正在試圖製作一個應用程序,在這個應用程序中我使用腳手架進行了位置模型,控制器和視圖。我在位置中有位置名稱和位置ID屬性。數據庫正在創建,位置表也在填充數據。現在我想創建一個部門類,我有3個屬性,分別是Dept_ID,Dept_Name和Loc_ID(外鍵)。我已在相應文件中添加了所需的代碼,如下所示。當我想從控制器訪問模型數據時,無法檢索我的模型類的元數據
在Department.cs(型號)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace DMS.Models
{
public class Department
{
public int D_ID { get; set; }
public string D_Name { get; set; }
[ForeignKey("L_ID")]
public int L_ID { get; set; }
}
}
在DB_CONTEXT類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace DMS.Models
{
public class DB_CONTEXT : DbContext
{
public DbSet<Location> Movies { get; set; }
public DbSet<Department> Department { get; set; }
}
}
和locatoin.cs(型號)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DMS.Models
{
public class Location
{
public int ID { get; set; }
public string L_Name { get; set; }
}
}
當我嘗試添加控制器部門我得到一個錯誤爲
unable to retrieve metadata for 'DMS.Models.Department' The navigation property 'L_ID' is not declared property on type Department.Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
嘗試此鏈接,這可能對您有所幫助:http://stackoverflow.com/a/14354851/1983024 –