操作工具
打开VS2017,选择工具->NutGet包管理器->程序包管理器控制台
关于命令
帮助
get-help entityframeworkcore
get-help Add-Migration
命令
Cmdlet | Description |
---|---|
Add-Migration | 添加新的迁移。 |
Drop-Database | 删除数据库。 |
Get-DbContext | 列出可用DbContext类型 |
Remove-Migration | 删除 (请回滚迁移已完成的代码更改) 的最后一个迁移。 |
Scaffold-DbContext | 为生成代码DbContext和数据库的实体类型。 为了使Scaffold-DbContext若要生成的实体类型,数据库表必须具有主键。 |
Script-Migration | 生成应用的所有更改从一个所选迁移到另一个所选的迁移的 SQL 脚本。 |
Update-Database | 更新数据库,到最后一个迁移或指定的迁移。 |
.netcore manytomany
https://blog.oneunicorn.com/2017/09/25/many-to-many-relationships-in-ef-core-2-0-part-1-the-basics/
https://blog.oneunicorn.com/2017/09/25/many-to-many-relationships-in-ef-core-2-0-part-2-hiding-as-ienumerable/
https://blog.oneunicorn.com/2017/09/25/many-to-many-relationships-in-ef-core-2-0-part-3-hiding-as-icollection/
https://blog.oneunicorn.com/2017/09/25/many-to-many-relationships-in-ef-core-2-0-part-4-a-more-general-abstraction/
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: The invoked member is not supported in a dynamic assembly.
如果启动项目使用 ASP.NET Core Web 主机 或 .Net Core 泛型主机,则这些工具将尝试从应用程序的服务提供程序获取 DbContext 对象。工具首先尝试通过调用
Program.CreateHostBuilder()
、调用Build()
,然后访问属性来获取服务提供程序Services
。
EF calls CreateWebHostBuilder or BuildWebHost without running Main. So Iconfiguration is null.
解决办法
Create new class which inherited from IDesignTimeDbContextFactory .
public class EmployeeContext : DbContext
{
//Dbcontext implementation
}
public class EmployeeFactory : IDesignTimeDbContextFactory<EmployeeContext>
{
public EmployeeContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<EmployeeContext>();
optionsBuilder.UseSqlServer("your connection string");
return new EmployeeContext(optionsBuilder.Options);
}
}
You are using a new .net core EF which uses IHostBuilder.(in an older version like yours the provider is IWebHostBuilder). The tools first try to obtain the service provider by invoking the Program.CreateHostBuilder(), calling Build(), then accessing the Services property.
EF needs to build the model and use the DbContext without starting the application. When EF invokes methods, your config services are still null that's why you get an error.
Make sure you have installed the package
Microsoft.EntityFrameworkCore.Tools
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。