我正在使用EF执行CRUD当前正在选择记录并将其传递给View View View。
表名和类名也是相同的,然后也无法理解为什么即使在调试之后这个错误也会出现同样的错误。
查看模型
namespace SingleVIewCrud.Models
{
public class tblClients
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
public class VMtblClient
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public List<tblClients> tblClients { get; set; }
}
}
Db Set
public virtual DbSet<tblClients> Clients { get; set; }
行动
public ActionResult Home(int? id)
{
VMtblClient model = new VMtblClient();
model.tblClients = db.Clients.ToList();
return View(model);
}
修改了Code DbContext类,
public partial class SampleEntities1 : DbContext
{
public SampleEntities1()
: base("name=SampleEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<tblClient> tblClients { get; set; }
public virtual DbSet<tblClients> tblClient { get; set; }
}
行动
public ActionResult Home(int? id)
{
VMtblClients model = new VMtblClients();
var test = db.tblClient.ToList();
return View(model);
}
答案 0 :(得分:0)
在SampleEntities1 DbContext类中,没有“客户端”的定义。所以“db.Clients.ToList();”不应该工作。你从哪里引用“db”,它必须是dbcontext类,
试试这段代码,
VMtblClient model = new VMtblClient();
using(var context = new SingleVIewCrud.Models.SampleEntities1())
{
model.tblClients = context.tblClient.ToList();
}
return View(model);