5

Sono nuovo di ASP.NET MVC e EF speriamo che questo non è una domanda stupidaerrore ASP.NET MVC/Entity Framework - nome di colonna non valido 'Environment_Id'

quando passo modello per ricercare altri mi sto questo errore - Dettagli eccezione: System.Data.SqlClient.SqlException: nome colonna non valido 'Environment_Id'.

Il modello o la tabella del database ha una proprietà con questo nome. Qualcuno potrebbe guidarmi su questo ?.

**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

Does ProfileVersion o ServerInfo hanno una proprietà Environment? –

+0

Sì, sono public int EnvironmentId {get; impostato; } @Olav Nybø – user2696668

+0

.... @ Olav Nybø – user2696668

risposta

10

Nei tuoi ProfileVersion e ServerInfo entità voi hanno una proprietà Environment navigazione. Per impostazione predefinita, Entity Framework proverà a creare una colonna del database denominata [Property Name]_[Referenced class PK]. Nel tuo scenario, questo è Environment_Id. Il problema, al momento, è che non hai fatto una migrazione per avere questa colonna del database creata.

Se dovessi immaginare cosa è successo qui, direi che hai creato le classi con EnvironmentId proprietà, la migrazione, poi deciso di aggiungere le proprietà di navigazione, Environment a ciascuno, in attesa EF associare che con il vostro attuale EnvironmentId proprietà. Ecco dove hai sbagliato. Come ho detto sopra, EF convenzione è quello di cercare una colonna di database denominato Environment_Id, quindi se si vuole EF utilizzare EnvironmentId invece, è solo bisogno di dire così con l'annotazione dei dati ForeignKey:

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

Ho aggiunto la notazione dei dati delle chiavi esterne per la proprietà EnvironmentId in ProfileVersion e ServeInfo ma ho ancora lo stesso problema .. @ Chris Pratt – user2696668

+0

La colonna 'EnvironmentId' nella tabella impostata come chiave esterna nel database livello? –

0

nel mio caso Ho aggiunto la mia relazione chiave primaria alla stessa chiave .. Quindi ho semplicemente rimosso ..

Problemi correlati