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

在我的情况我已将我的主键关系添加到相同的键..所以我只是删除..