【发布时间】:2013-12-20 11:54:01
【问题描述】:
我在我的 asp.net MVC 4 应用程序中使用实体框架。我有一个 edmx 文件。我正在尝试使用此 EF 模型中的实体来填充这样的视图模型:
using (var context = new VehiclesContext())
{
IEnumerable<SearchedVehicles> vehicles = context.Vehicles.Select(x => new SearchedVehicles
{
Year = x.Year,
Make = x.Make,
Model = x.Model,
Mileage = x.Mileage,
VIN = x.VIN
});
return View(vehicles);
}
Vehicles 是 edmx 中的实体,其中 SearchedVehicles 是视图模型,但我得到了这个例外:
Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
在这一行:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework asp.net-mvc-4