我正在使用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