栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

详解如何在ASP.NET Core中应用Entity Framework

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详解如何在ASP.NET Core中应用Entity Framework

首先为大家提醒一点,.NET Core和经典.NET framework的Library是不通用的,包括Entity framework!

哪怎么办? 别急,微软为.NET Core发布了.NET Core版本的Entity framework,具体配置方法与经典.NET framework版本的稍有区别,下面的内容就为带领大家在ASP.NET Core中应用Entity framework DB first。

注:目前部分工具处于Preview版本,正式版本可能会稍有区别。

 前期准备:

1.推荐使用VS2015 Update3作为你的IDE,下载地址:https://www.jb51.net/softjc/446184.html

2.你需要安装.NET Core的运行环境以及开发工具,这里提供VS版:https://www.jb51.net/softs/472362.html

3.你需要有一个Sql Server数据库。

结构应该是这样的。

CREATE DATAbase TestNetCoreEF 
GO 
USE TestNetCoreEF 
GO 
CREATE TABLE Student( 
  ID int identity primary key, 
  Name nvarchar(50), 
  Age int 
) 
 
INSERT INTO Student VALUES('Bear',18) 
INSERT INTO Student VALUES('Frank',20)

创建项目

在VS中新建项目,项目类型选在ASP.NET Core Web Application (.NET Core),输入项目名称为TestEFInNetCore

接下来选择Web Application, 右侧身份认证选择:No Authentication

安装Entity framework

打开Tool->NuGet Package Manager->Package Manager Console

在Pack Manager Console中运行如下命令:

  Install-Package Microsoft.EntityframeworkCore.SqlServer

  Install-Package Microsoft.EntityframeworkCore.Tools –Pre

  Install-Package Microsoft.EntityframeworkCore.SqlServer.Design

打开Project.json,在节点tool中添加如下配置:

"tools": { 
  "Microsoft.EntityframeworkCore.Tools": "1.0.0-preview2-final", 
  …………. 
}

这是VS会自动下载对应的包至你的本地,目前这个还是preview版本,正式版请关

注:https://docs.efproject.net/en/latest/intro.html

生成数据库Mapping

在Pack Manager Console中于运行如下命令:

Scaffold-DbContext "{Your DB connect string}" Microsoft.EntityframeworkCore.SqlServer -OutputDir Models

{Your DB connect string}:你的数据库连接字符串

Microsoft.EntityframeworkCore.SqlServer:目标数据库为Sql Server

-OutputDir Models: 生成的文件的存放目录,目前目录是根目录下的Models目录

之后引擎会试图连接你的SQL Server 数据库,并生成文件在你指定的目录里。

在目录中找到一个***Context.cs并打开它,你会发现一个如下方法,

protected override void onConfiguring(DbContextOptionsBuilder optionsBuilder)
{
  #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?linkId=723263 for guidance on storing connection strings.
  optionsBuilder.UseSqlServer(@"{your sql connect string}");
}

如自动生成代码里所写的warning一样,我们不应该把连接字符串放在这里。接下来的工作,让我们来从appsettings.json中读取配置。

在***Context.cs中添加一个属性用来存放ConnectionString,另外我们需要重写OnConfiguring方法,完整的代码应该是这样:

public static string ConnectionString { get; set; } 
protected override void onConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 
  optionsBuilder.UseSqlServer(ConnectionString); 
}

打开appSetting.json,添加如下代码:

"ConnectionStrings": { 
  "TestNetCoreEF": "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};" 
},

完整的代码应该像这样:

{ 
  "ConnectionStrings": { 
    "TestNetCoreEF": "Data Source={your sql server host address};Initial Catalog=TestNetCoreEF;user id={your username};password={your password};" 
  }, 
  "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
      "Default": "Debug", 
      "System": "Information", 
      "Microsoft": "Information" 
    } 
  } 
}

打开 Startup.cs,在ConfigureServices(IServiceCollection services)方法中添加如下代码:

TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF");

完整的代码应该是这样:

public void ConfigureServices(IServiceCollection services) 
{ 
  //config the db connection string 
  TestNetCoreEFContext.ConnectionString = Configuration.GetConnectionString("TestNetCoreEF"); 
 
  // Add framework services. 
  services.AddMvc(); 
}

关于调用Entity framework

真的,相信我,跟之前一毛一样,真的一毛一样。

Models.TestNetCoreEFContext context = new Models.TestNetCoreEFContext();

var StudentList = context.Student.ToList();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/55926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号