gpt4 book ai didi

c# - 如果在代码优先模式下使用,使用数据库优先和模型优先开发的 T4 模板生成的代码可能无法正常工作

转载 作者:太空狗 更新时间:2023-10-30 00:24:54 25 4
gpt4 key购买 nike

<分区>

我在我的 asp.net MVC 4 应用程序中使用 Entity Framework 。我有一个 edmx 文件。我正在尝试使用此 EF 模型中的实体来填充这样的 View 模型:

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 是 viewmodel 但我得到这个异常:

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();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com