5

Jestem nowym ASP.NET MVC i EF nadzieją nie jest to głupie pytanieASP.NET MVC/Entity Framework Błąd - Nieprawidłowa nazwa kolumny 'Environment_Id'

Kiedy mijam modelu, aby zobaczyć Dostaję ten błąd - Szczegóły wyjątku: System.Data.SqlClient.SqlException: Niepoprawna nazwa kolumny "Environment_Id".

Tabela modeli lub bazy danych ma właściwość o tej nazwie. Czy ktoś mógłby mnie o tym poinformować ?.

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

Czy ProfileVersion lub ServerInfo mają właściwość środowiska? –

+0

Tak -they public int EnvironmentId {get; zestaw; } @Olav Nybø – user2696668

+0

.... @ Olav Nybø – user2696668

Odpowiedz

10

W swoim ProfileVersion i ServerInfo podmiotów masz nieruchomość Environment nawigacji. Domyślnie Entity Framework spróbuje utworzyć kolumnę bazy danych o nazwie [Property Name]_[Referenced class PK]. W twoim scenariuszu jest to Environment_Id. W tej chwili problem polega na tym, że nie wykonałeś migracji, aby utworzyć tę kolumnę bazy danych.

Gdybym miał sobie wyobrazić, co się tu stało, powiedziałbym najpierw stworzył zajęcia z EnvironmentId właściwości, migracji, a potem postanowił dodać właściwości nawigacyjnych, Environment do siebie, oczekując, że EF powiązać z istniejącym EnvironmentId nieruchomości. Tam właśnie popełniłeś błąd. Jak już wspomniano, konwencja EF jest spojrzenie na kolumny bazy danych o nazwie Environment_Id, więc jeśli chcesz EF używać EnvironmentId zamiast, po prostu trzeba powiedzieć to tak z dopiskiem ForeignKey danych:

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

Dodałem notację danych klucza obcego dla właściwości EnvironmentId w ProfileVersion i ServeInfo, ale nadal mam ten sam problem. @ Chris Pratt – user2696668

+0

Czy kolumna 'EnvironmentId' w tabeli jest ustawiona jako klucz obcy w bazie danych poziom? –

0

w moim przypadku Dodałem mój klucz podstawowy związek do tego samego klucza. Tak po prostu usunąć ..

Powiązane problemy