ASP.NET MVC /Entity Framework Error - Invalid column name 'Environment_Id'

前端 未结 3 1996
生来不讨喜
生来不讨喜 2020-12-15 13:07

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.SqlC

3条回答
  •  粉色の甜心
    2020-12-15 13:44

    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; }
    

提交回复
热议问题