我正在使用Entity Framwwork和Code First,並且變得非常困惑。我有這個類:實體框架與`虛擬'混淆
public class Blocks
{
[Display(Name = "ID"),Required(ErrorMessage = "ID is required")]
[Key,HiddenInput(DisplayValue=false)]
public int BlockId { get;set; }
[Display(Name = "Blocked By"),Required(ErrorMessage = "Blocked By is required")]
public int ProfileId { get;set; }
[Display(Name = "Blocked"),Required(ErrorMessage = "Blocked is required")]
public int ProfileBlockedId { get;set; }
[Display(Name = "Date Blocked"),Required(ErrorMessage = "Date Blocked is required")]
public DateTime BlockDateTime { get;set; }
[Display(Name = "Block Reason")] public string BlockReason { get;set; }
public virtual Profiles Profile { get; set; }
public virtual Profiles ProfileBlocked { get; set; }
}
配置文件類或多或少相同,並且增加了罰款和具有正確的SQL,但是當我運行/塊我得到這個錯誤:
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'Extent1.Profile_ProfileId' in 'field list'
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
這是因爲所產生的SQL是:
SELECT
`Extent1`.`BlockId`,
`Extent1`.`ProfileId`,
`Extent1`.`ProfileBlockedId`,
`Extent1`.`BlockDateTime`,
`Extent1`.`BlockReason`,
`Extent1`.`Profile_ProfileId`,
`Extent1`.`ProfileBlocked_ProfileId`
FROM `Blocks` AS `Extent1`
通知的Profile_
和ProfileBlocked_
。我將它們設置爲虛擬,以便在添加或編輯時具有配置文件下拉列表,或者在列表中顯示時具有配置文件名稱。奇怪的是其他桌子。除了這個之外,一切都運行良好。
下面是創建錯誤的SQL和斷碼:
//
// GET: /Blocks/
public ViewResult Index()
{
try {
return View(context.Blocks.Include(blocks => blocks.Profile).Include(blocks => blocks.ProfileBlocked).ToList());
}
catch (Exception ex)
{
ModelState.AddModelError("",ex.Message);
CompileAndSendError(ex);
return View(context.Blocks.ToList());
}
}
我使用:
ASP.net MVC 3
剃刀模板
實體框架
MVC腳手架[自定義T4]
令人驚歎的是,我以前做過的外鍵的技巧非常出色,但從來不知道用什麼方法指向IE瀏覽器引用內部名稱或引用您引入的類的ID。是否知道是否有可能擁有一個多個鍵的配置文件?哦,非常感謝 – davethecoder 2011-06-03 22:54:01
@ minus4:你對「多個鍵的一個配置文件」是什麼意思?但我有這樣的感覺,答案是否定的,這是不可能的;)或者創建一個關於這個新的問題...順便說一句:我已經附加了一個關於放置FK屬性的替代編輯。 – Slauma 2011-06-03 23:35:26
mucho gracias :-) – davethecoder 2011-06-04 10:02:28