.net Core 中调用 EF Core项目

首先得建一个类库供.net Core 的主项目引用;其次,在.net Core 中做注入;最后,在页面中调用

1、类库项目中的操作

   1.新建类库项目

   2.新建Entity Class,表名为:Products Entity Class Name :Product

   3.Create DBContext File:

      1.Create Construction 构造函数 DBContext File name:MyDBContext 继承:DBContext 构造函数::base(opt)实质是为了.net Core 注入传参 public MyDBContext(DbContextOptions<MyDBContext> opt):base(opt){}

2.Add DbContext 属性 注意:属性名称与表明一致 public DbSet<Products> Products { get; set; }

public class MyDBContext:DbContext
{
public MyDBContext(DbContextOptions<MyDBContext> opt) :base(opt)
{
}

public DbSet<Product> Products { get; set; }
...
}

  

2、.netCore 中引用 Startup.cs中ConfigureServices 注入EF Core服务

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<Sport.Entity.SportStoreDBContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("SportDB"))//appsettings.json ConnectionStrings.SportDB
) ;
...
}

  

appsettings.json 中配置ConnectionStrings 节点值

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SportDB": "Server=(localdb)\\ProjectsV13;database=MyPracticeDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}

3 页面中调用,net Core 将EF服务,以构造函数的方式提供调用

private readonly SportStoreDBContext _sportContext;

public HomeController(SportStoreDBContext sportContext)
   {
    _sportContext = sportContext;
   }

//调用

public ActionResult List()
{

var list = _sportContext.Product.ToList();

}

  

最新文章

  1. Vertica 分区表设计
  2. 【解题报告】BZOJ2550: [Ctsc2004]公式编辑器
  3. &lt;input type=&quot;file&quot;&gt; change事件异常处理办法
  4. Device Path in WinPrefetchView
  5. ubuntu14安装java8
  6. 转发 python中file和open有什么区别
  7. Tomcat部署Web应用方法总结
  8. 从算法入手讲解如何在SQL Server中实现最优最简
  9. 自动生成 Makefile (automake/autoconf 入门)
  10. sfdfssd
  11. ES6的Iterator遍历器
  12. msf入门学习笔记
  13. 理解rem实现响应式布局原理及js动态计算rem
  14. selenium chrome 自动加载flash
  15. 序列化模块json--pickle--shelve
  16. Linux中两台主机配置互信关系
  17. WebApi Session支持
  18. ajax 动态载入html后不能执行其中的js解决方法
  19. Hadoop + HBase (自带zookeeper 也可单独加) 集群部署
  20. mac通过wine运行windows程序(将文件关联到windows程序notepad++)

热门文章

  1. Linux命令安装Mysql
  2. 十年OI一场空,不开long long见祖宗
  3. Python解释器安装、多版本共存以及手动添加环境变量教程
  4. Solution -「AT 3913」XOR Tree
  5. 我来教你如何将cpu使用率up起来(shell脚本[含注释])
  6. c++ TextQuery程序
  7. Windows查看本机SSH公钥,生成公钥
  8. Spring Boot自动配置SpringMVC(一)
  9. 针对Office宏病毒的高级检测
  10. SQL Server 索引结构