如何在ASP.NET Core项目中设置实体框架 [英] How to setup Entity Framework in ASP.NET Core project

查看:181
本文介绍了如何在ASP.NET Core项目中设置实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新的ASP.NET Core项目,我试图在其中简单地查询数据库并将查询结果显示在页面上.我是使用ASP.NET Core的新手,从研究中发现,最好使用实体框架来查询数据库.

I have a new ASP.NET Core project where I am trying to simply query a database and display results of the query on a page. I'm new to using ASP.NET Core and have found from researching that it would be best to use Entity Framework in order to query the database.

我试图按顺序遵循本指南以确定如何通过Entity Framework连接到数据库,但是指南中有一个步骤引用了名为project.json的文件,并在其中添加了行:"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",.

I was trying to follow this guide in order to figure out how to connect to the database via the Entity Framework, but there is a step in the guide that references a file called project.json to which the line: "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final", should be added.

问题是,如本指南,在我的项目中,文章引用的.csproj文件不能替代project.json.

The problem is that this file doesn't exist in Visual Studio 2017 projects as is confirmed in this guide and I don't see the .csproj file anywhere in my project that the article references as the replacement for project.json.

.csproj文件是否应该在创建项目之后创建?在Visual Studio 2017中通过ASP.NET Core项目中的实体框架建立与数据库的连接的实际过程是什么?

Is this .csproj file supposed to be created after the project has already been created? What is the actual process for setting up a connection to a database via the Entity Framework in an ASP.NET Core project within Visual Studio 2017?

推荐答案

当我学习如何创建ASP.NET Core项目时,我的老师建议我们遵循以下

When I was learning how to create a ASP.NET Core project, my teacher suggested us to follow this guide from the MS docs. I suggest you to follow that guide to solve your problem. In that guide says that you need to add the connection string of your database to the file appsettings.json that's located in your projects main folder. The connection string is like this:

"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=YourDatabaseName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

然后将连接字符串添加到文件appsettings.json中,如下所示:

"ConnectionStrings": {
"DB_String": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=YourDatabaseName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},

然后,您必须通过添加以下行来更改项目主文件夹中Startup.cs文件中的ConfigureServices方法:

Then you have to change the ConfigureServices method at the Startup.cs file in your project main folder by adding the following line:

services.AddDbContext<yourContextClass>(options => options.UseSqlServer(Configuration.GetConnectionString("DB_String")));

然后该方法将如下所示:

And then the method will seen like this:

public void ConfigureServices(IServiceCollection services)
   {
       services.AddDbContext<yourContextClass>(options => options.UseSqlServer(Configuration.GetConnectionString("DB_String")));
       services.AddMvc();
   }

这些步骤是使用SQL Server客户端使用ASP.NET Core 2配置与Entity Framework的数据库连接的步骤.对于下一步,建议您遵循

That are the steps to configurate with ASP.NET Core 2 a database connection with Entity Framework by using the SQL Server client. For the next steps I suggest you to follow this guide.

这篇关于如何在ASP.NET Core项目中设置实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆