Entity Framework

万马奔腾
• 阅读 1562

操作工具

打开VS2017,选择工具->NutGet包管理器->程序包管理器控制台

关于命令

微软官方链接

帮助

get-help entityframeworkcore  
get-help Add-Migration

命令

CmdletDescription
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
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
NO.A.0006——DOS常用命令
一、DOS常用命令操作:1、常用操作示例:winR键打开运行命令行:启动DOS窗口:sndrec32-------录音机Nslookup-------IP地址侦测器explorer-------打开资源管理器logoff---------注销命令tsshutdn--
Easter79 Easter79
4年前
TiDB Pre
8月30日,TiDB发布PreGA版。该版本对MySQL兼容性、SQL优化器、系统稳定性、性能做了大量的工作。TiDB:SQL查询优化器调整代价模型优化索引选择,支持不同类型字段比较的索引选择支持基于贪心算法的JoinReorder
Wesley13 Wesley13
4年前
HTTP面试题(二):HTTP请求报文和响应报文格式
!(https://oscimg.oschina.net/oscnet/0406894fb1274bee91fc53c84c516576.jpg)看都看了还不点个赞!(https://oscimg.oschina.net/oscnet/095d444dc9a449ee85afd19b00fdf52b.png)!(h
Stella981 Stella981
4年前
Pre
PAT甲级1119,我先在CSDN上面发布的这篇文章:https://blog.csdn.net/weixin\_44385565/article/details/89737224(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.csdn.net%2Fweixin_443855
Stella981 Stella981
4年前
Autojs的intent使用
以打开淘宝app的搜索页面为例,先准备工具:Activity管理器下载地址:https://www.cr173.com/soft/824332.html备用地址:https://pan.baidu.com/s/1l2266vDbzp0hYcchkUC5jg提取码:qeve打开管理器!(https://oscimg.oschina
Stella981 Stella981
4年前
Postman的安装
在网上下载一个Postman插件,1.首先用户点击谷歌浏览器右上角的自定义及控制按钮,在下拉框中选择工具选项,然后点击扩展程序来启动Chrome浏览器的扩展管理器页面。!打开Chrome扩展管理器(http://chromecj.com/Content/kindeditor/attached/image/20140926/201409262348
Wesley13 Wesley13
4年前
MongoDB 范围查询
查询价格在2009000  $gt 大于   $lt  小于//查询价格2009000范围的数据db.prodgory.find({"price":{$gt:"200",$lt:"9000"}})查询给定范围数据  $in//给定范围查询db.product1.find({"categor
Stella981 Stella981
4年前
Python 为什么只需一条语句“a,b=b,a”,就能直接交换两个变量?
从接触Python时起,我就觉得Python的元组解包(unpacking)挺有意思,非常简洁好用。最显而易见的例子就是多重赋值,即在一条语句中同时给多个变量赋值:&gt;&gt;&gt;x,y1,2&gt;&gt;&gt;print(x,y)结果:12在此例中,赋值操作符“”号的右
小万哥 小万哥
1年前
RSS 解析:全球内容分发的利器及使用技巧
RSS(ReallySimpleSyndication)是一种XML格式,用于网站内容的聚合和分发,让用户能快速浏览和跟踪更新。RSS文档结构包括&lt;channel&gt;和&lt;item&gt;元素,允许内容创作者分享标题、链接和描述。通过RSS,用户可以定制新闻源,过滤不相关信息,提高效率。RSS支持不同版本,如RSS0.91和RSS2.0,其中RSS2.0语法简单且广泛使用。RSS提高网站流量,适用于新闻、博客、日历等频繁更新的站点。RSS的历史始于1997年,至今仍无官方标准,但已成为内容共享的重要工具。