ASP.NET MVC /entity framework错误 – 无效的列名称’Environment_Id’
我是ASP.NET MVC和EF的新手,希望这不是一个愚蠢的问题
当我传递模型来查看我收到此错误 – exception详细信息:System.Data.SqlClient.SqlException:无效的列名称’Environment_Id’。
模型或数据库表具有该名称的属性。 可以指导我吗?
**Here is the Version Model Class** public partial class Version { public Version() { this.ProfileVersions = new List(); this.ServerInfoes = new List(); } 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 ProfileVersions { get; set; } public virtual ICollection ServerInfoes { get; set; } } **Profile Version Class** public partial class ProfileVersion { public ProfileVersion() { this.PlatformConfigurations = new List(); } public int Id { get; set; } public int ProfileId { get; set; } public int EnvironmentId { get; set; } public int VersionId { get; set; } public Nullable Locked { get; set; } public string LockedBy { get; set; } public string Comments { get; set; } public Nullable Active { get; set; } public virtual Environment Environment { get; set; } public virtual ICollection 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(); } 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 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 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 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 LastUpdate { get; set; } public virtual Environment Environment { get; set; } public virtual ICollection 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" ) )
在您的ProfileVersion
和ServerInfo
实体中,您具有Environment
导航属性。 默认情况下,Entity Framework将尝试创建名为[Property Name]_[Referenced class PK]
的数据库列。 在您的方案中,那是Environment_Id
。 现在的问题是,您尚未完成迁移以创建此数据库列。
如果我不得不想象这里发生了什么,我会说你先创建了带有EnvironmentId
属性的类,进行了迁移,然后决定向每个属性添加导航属性Environment
,期望EF将它与你现有的EnvironmentId
属性相关联。 那是你出错的地方。 如上所述,EF约定是查找名为Environment_Id
的数据库列,因此如果您希望EF使用EnvironmentId
,您只需要使用ForeignKey
数据注释告诉它:
[ForeignKey("Environment")] public int EnvironmentId { get; set; }
在我的情况下,我已将我的主键关系添加到相同的键..所以我只是删除..
我意识到这个问题现在已经有3年了,但我看到了错误的另一个原因 – 在原始问题和我自己的代码中都非常相似。 而且,在我的情况下,我有与上述相同的错误。
我有一个带有ID和Name对的“MY_ACTIONS”表,我希望将其添加到下拉列表中。 这是模型:
namespace TestSite.Models { public class MY_ACTIONS { //[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public MY_ACTIONS() { this.o_actions = new HashSet(); } [Key] public int action_id { get; set; } [StringLength(100)] public string action_name { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection o_actions { get; set; } } }
要获取要在下拉列表中显示的操作,它在我的主表中的一个名为LASTACTION
的int
字段中设置了一个ID。 在那个模型中,我宣布了ForeignKey关系:
namespace TestSite.Models { [Table("MAIN_TABLE")] public partial class MAIN_TABLE { [Key] public int MAIN_TABLE_ID { get; set; } public int LASTACTION { get; set; } // this would carry a number matching action_id [ForeignKey("LASTACTION")] public virtual MY_ACTIONS MY_ACTIONS { get; set; } } }
在我的视图中加载此下拉列表时出现错误Invalid column name 'MY_ACTIONS_action_id'
错误:
@Html.DropDownList("lastaction", null, htmlAttributes: new { @class = "form-control" })
…我在Controller函数中使用此ViewBag:
Model1 db = new Model1(); // database context MAIN_TABLE o_main = new MAIN_TABLE(); o_main.lastaction = 2; ViewBag.lastaction = new SelectList(db.MY_ACTIONS, "action_id", "action_name", o_main.lastaction);
如果我没有宣布我的FK关系:
[ForeignKey("LASTACTION")] public virtual MY_ACTIONS MY_ACTIONS { get; set; }
我可能也会遇到同样的问题。 拥有虚拟实例的表示需要将其与某些物理属性相关联。 这类似于:
public virtual Environment Environment { get; set; }
应该:
[ForeignKey("EnvironmentId")] public virtual Environment Environment { get; set; }
在上面的问题中的ProfileVersion
类中,假设EnvironmentId
是名为Environment
的表中的主键(该模型未在上面显示)。
但对我来说,我已经有了这个,我仍然得到错误,所以这样做仍然可能无法解决所有问题。
事实certificate,我必须做的就是摆脱MY_ACTIONS
模型中的ICollection
和this.o_actions = new HashSet
这一切都很顺利。
在上面的问题中有许多这样的列表和ICollections
正在发挥作用,所以我也打赌有一些错误。 从仅表示字段的普通模型开始,然后添加表示与外键链接的表的虚拟对象。 然后确保您的下拉列表加载。 只有在那之后你才应该开始添加你的ICollections
, HashSets
, Lists
和其他实际上并不是数据库实际部分的设施 – 这可能HashSets
entity framework认为它需要与它们做一些它没有做的事情。我需要这样做。