Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to ASP.NET MVC and EF hopefully this is not a silly question

When i pass model to view i'm getting this error - Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Environment_Id'.

Model nor database table has a property by that name. Could any guide me on this?.

  **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"
                )
            )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

In your ProfileVersion and ServerInfo entities you have an Environment navigation property. By default, Entity Framework will try to create a database column called [Property Name]_[Referenced class PK]. In your scenario, that's Environment_Id. The problem, right now, is that you have not done a migration to have this database column created.

If I had to imagine what happened here, I'd say you first created the classes with EnvironmentId properties, migrated, then later decided to add the navigation properties, Environment to each, expecting EF to associate that with your existing EnvironmentId properties. That's where you went wrong. As I said above, EF convention is to look for a database column named Environment_Id, so if you want EF to use EnvironmentId instead, you just need to tell it so with the ForeignKey data annotation:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...