Rob*_*abe 6 c# ef-migrations entity-framework-core asp.net-core
我在尝试将Entity Framework Core的迁移添加到Code First项目时遇到错误,这里有详细信息......
我创建了一个新的ASP.Net核心Web项目(VS 2017中的Core 2.0).它使用Microsoft.AspNetCore.All依赖项,如下所示:
我希望利用实体框架核心(我的理解是,所有元数据都已包含EF Core依赖项,如下所示,看起来是正确的):
我已经设置了我的实体和上下文,并且我确保使用以下代码设置数据库.
示例模型
public class City
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
[MaxLength(200)]
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
示例上下文
public class CityInfoContext : DbContext
{
public DbSet<City> Cities { get; set; }
public DbSet<PointOfInterest> PointsOfInterest { get; set; }
public CityInfoContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.cs配置
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddMvcOptions(options => {
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
})
.AddJsonOptions(options => {
if (options.SerializerSettings.ContractResolver != null)
{
var res = options.SerializerSettings.ContractResolver as DefaultContractResolver;
res.NamingStrategy = null;
}
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IMailService, LocalMailService>();
// Entity Framework services.
var connectionString = @"Server=(localdb)\mssqllocaldb;Database=CityInfoDB;Trusted_Connection=True;";
services.AddDbContext<CityInfoContext>(options => options.UseSqlServer(connectionString));
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中使用此行初始化db conext:
public class DummyController : Controller
{
CityInfoContext _ctx;
public DummyController(CityInfoContext ctx)
{
_ctx = ctx;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以看到db已经成功创建 - 到目前为止一切都很好.
我想使用此命令拍摄我的数据库的快照: PM> Add-Migration CityInfoInitialMigration
但得到错误: 项目'CityInfo.API'上没有安装EntityFramework包.
以前有人遇到过这个吗?我明确尝试添加EF包,但这也不起作用!
NPM> Get-Module
Run Code Online (Sandbox Code Playgroud)
如果结果包含 EntityFramework
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 6.0.0.0 EntityFramework {Add- EFDefaultConnectionFactory, Add-EFProvider, Add-Migration, Enable-Migrations...}
Script 2.0.0 EntityFrameworkCore {Add-Migration, Drop-Database, Enable-Migrations, Get-DbContext...}
Script 2.0.0.0 NuGet {Add-BindingRedirect, Find-Package, Get-Package, Get-Project...}
Script 0.0 profile
Run Code Online (Sandbox Code Playgroud)
这意味着EntityFrameworkCore和EntityFrameworkNuget软件包都已安装在项目中,这会导致
未安装EntityFramework软件包
在我的情况下,我正在引用某个正在引用的Nuget包EntityFramework 6.0.0(因此该EntityFramework包被间接引用)。删除该软件包后,该错误已修复。
查找此类参考的最简单方法是使用Search Solution Explorer对话框
| 归档时间: |
|
| 查看次数: |
6602 次 |
| 最近记录: |