![](/img/trans.png)
[英]Invalid column name ProveedoresID and UsuarioID on ASP.NET MVC 3 web app using Entity Framework Code First
[英]ASP.NET MVC /Entity Framework Error - Invalid column name 'Environment_Id'
我是ASP.NET MVC和EF的新手,希望這不是一個愚蠢的問題
當我傳遞模型來查看我收到此錯誤 - 異常詳細信息:System.Data.SqlClient.SqlException:無效的列名稱'Environment_Id'。
模型或數據庫表具有該名稱的屬性。 可以指導我嗎?
**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"
)
)
在您的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年了,但是我看到了錯誤的原因 - 原始問題和我自己的代碼非常相似。 而且,在我的情況下,我有與上述相同的錯誤。
我有一個“MY_ACTIONS”表,其中包含ID和Name對,我希望將其添加到下拉列表中。 這是模型:
namespace TestSite.Models
{
public class MY_ACTIONS
{
//[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public MY_ACTIONS()
{
this.o_actions = new HashSet<MY_ACTIONS>();
}
[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<MY_ACTIONS> 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
的表中的主鍵(該模型未在上面顯示)。
但對我來說,我已經有了這個,我仍然得到錯誤,所以這樣做仍然可能無法解決所有問題。
事實證明我所要做的就是擺脫MY_ACTIONS
模型中的ICollection<MY_ACTIONS> o_actions
和this.o_actions = new HashSet<MY_ACTIONS>();
這一切都很順利。
在上面的問題中有很多這樣的列表和ICollections
正在發揮作用,所以我也打賭有一些東西是錯誤的。 從僅表示字段的普通模型開始,然后添加表示與外鍵鏈接的表的虛擬對象。 然后確保您的下拉列表加載。 只有在那之后你才應該開始添加你的ICollections
, HashSets
, Lists<T>
和其他實際上並不是數據庫實際部分的設施 - 這可能HashSets
實體框架認為它需要與它們做一些它沒有做的事情。我需要這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.