5

我新的ASP.NET MVC和EF希望這不是一個愚蠢的問題ASP.NET MVC /實體框架錯誤 - 無效的列名稱Environment_Id「

當我通過模型來查看我越來越此錯誤 - 異常詳細信息:System.Data.SqlClient.SqlException:無效的列名稱'Environment_Id'。

模型和數據庫表具有該名稱的屬性。有沒有人能指導我呢?

**Here is the Version Model Class** 

    public partial class Version 
    { 
    public Version() 
    { 
     this.ProfileVersions = new List<ProfileVersion>(); 
     this.ServerInfoes = new List<ServerInfo>(); 
    } 

    public int Id { get; set; } 
    public string Number { get; set; } 
    public string Tag { get; set; } 
    public string Owner { get; set; } 
    public string Approver { get; set; } 
    public string Description { get; set; } 
    public virtual ICollection<ProfileVersion> ProfileVersions { get; set; } 
    public virtual ICollection<ServerInfo> ServerInfoes { get; set; } 
} 

**Profile Version Class** 

public partial class ProfileVersion 
{ 
    public ProfileVersion() 
    { 
     this.PlatformConfigurations = new List<PlatformConfiguration>(); 
    } 

    public int Id { get; set; } 
    public int ProfileId { get; set; } 
    public int EnvironmentId { get; set; } 
    public int VersionId { get; set; } 
    public Nullable<bool> Locked { get; set; } 
    public string LockedBy { get; set; } 
    public string Comments { get; set; } 
    public Nullable<int> Active { get; set; } 
    public virtual Environment Environment { get; set; } 
    public virtual ICollection<PlatformConfiguration> PlatformConfigurations { get; 
                      set; } 
    public virtual PlatformProfile PlatformProfile { get; set; } 
    public virtual Version Version { get; set; } 
} 

**ServerInfo** 
public partial class ServerInfo 
{ 
    public ServerInfo() 
    { 
     this.PlatformConfigurations = new List<PlatformConfiguration>(); 
    } 

    public int Id { get; set; } 
    public string ServerName { get; set; } 
    public int ProfileId { get; set; } 
    public int VersionId { get; set; } 
    public int EnvironmentId { get; set; } 
    public string ServerType { get; set; } 
    public Nullable<short> Active { get; set; } 
    public string Domain { get; set; } 
    public string Location { get; set; } 
    public string IP { get; set; } 
    public string Subnet { get; set; } 
    public string Gateway { get; set; } 
    public Nullable<int> VLan { get; set; } 
    public string DNS { get; set; } 
    public string OS { get; set; } 
    public string OSVersion { get; set; } 
    public string Func { get; set; } 
    public Nullable<short> IISInstalled { get; set; } 
    public string ADDomainController { get; set; } 
    public string ADOrganizationalUnit { get; set; } 
    public string ADGroups { get; set; } 
    public string LastError { get; set; } 
    public Nullable<System.DateTime> LastUpdate { get; set; } 
    public virtual Environment Environment { get; set; } 
    public virtual ICollection<PlatformConfiguration> PlatformConfigurations { get;  
                      set; } 
    public virtual PlatformProfile PlatformProfile { get; set; } 
    public virtual Version Version { get; set; } 
    public virtual VMConfiguration VMConfiguration { get; set; } 
} 

**Controller Code-** 

public ViewResult Index(string id) 
    { 

     var profileVerList = from ver in _context.Versions 
           where !(from pfv in _context.ProfileVersions 
            select pfv.VersionId).Contains(ver.Id) 
           select ver; 

     var bigView = new BigViewModel 
     { 
      VersionModel = profileVerList.ToList(),     
     }; 

     return View(model: bigView); 
    } 


**In the View where the exception is thrown** 

@Html.DropDownList(
      "SelectedVersionID", 
      new SelectList(
       Model.VersionModel.Select(x => new { Value = x.Id, Text = x.Number}), 
       "Value", 
       "Text" 
       ) 
      ) 
+1

ProfileVersion或ServerInfo是否有環境屬性? –

+0

是的-they public int EnvironmentId {get;組; } @OlavNybø – user2696668

+0

.... @ OlavNybø – user2696668

回答

10

在你ProfileVersionServerInfo實體你有一個Environment導航屬性。默認情況下,實體框架將嘗試創建一個名爲[Property Name]_[Referenced class PK]的數據庫列。在你的場景中,這是Environment_Id。現在的問題是,您尚未進行遷移來創建此數據庫列。

如果我不得不想象這裏發生了什麼事,我說你先用EnvironmentId屬性創建的類,遷移,然後再決定增加導航性能,Environment每個,期待EF關聯與您現有的EnvironmentId屬性。那是你出錯的地方。正如我前面所說,EF慣例是,尋找名爲Environment_Id數據庫列,所以如果你想EF使用EnvironmentId相反,你只需要與ForeignKey數據註解來告訴它這樣:

[ForeignKey("Environment")] 
public int EnvironmentId { get; set; } 
+0

我在ProfileVersion和ServeInfo中爲EnvironmentId屬性添加了外鍵數據表示法,但仍然有相同的問題.. @ Chris Pratt – user2696668

+0

表中的EnvironmentId列是否設置爲數據庫中的外鍵水平? –

0

在我的情況我已將我的主鍵關係添加到相同的鍵..所以我只是刪除..

相關問題