【发布时间】:2016-01-27 11:23:31
【问题描述】:
我在两个表中的插入成功,但在显示数据的索引操作时出现错误。
模型和视图模型。
查看模型
public class VMUsers
{
public int Id { get; set; }
public string FullName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
}
型号
[Table("tblUsers")]
public class Users
{
[Key]
public int Id { get; set; }
public string FullName { get; set; }
public string LastName { get; set; }
}
[Table("tblUserDetails")]
public class UserDetails
{
[Key]
public int Id { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public int UserID { get; set; }
}
数据库集
public System.Data.Entity.DbSet<MVCLearning.Models.VMUsers> Combination { get; set; }
索引操作
public ActionResult Index()
{
return View(db.Combination.ToList());
}
获取异常
EntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常,但未在用户代码中处理
附加信息:执行命令定义时出错。有关详细信息,请参阅内部异常。
无法理解这个内部异常是什么。
它会中断,因此查看错误的调试部分也会中断,它不会转到视图进行调试。
【问题讨论】:
-
VMUsers是视图模型,而不是数据模型。它应该与 EF 没有任何关系。 -
另外,你确定你的实体模型定义是正确的吗?表之间有足够的外键关系吗?
-
好像现在正在练习在两个表中添加数据,所以保持简单,没有保留 PK 和 FK,但实时 PK 和 FK 会在那里。
标签: c# entity-framework asp.net-mvc-4