【问题标题】:How to setup Entity Framework in ASP.NET Core project如何在 ASP.NET Core 项目中设置实体框架
【发布时间】:2018-04-23 00:58:37
【问题描述】:

我有一个新的 ASP.NET Core 项目,我试图在其中简单地查询数据库并在页面上显示查询结果。我是使用 ASP.NET Core 的新手,并且通过研究发现最好使用 Entity Framework 来查询数据库。

我试图关注this guide 以了解如何通过实体框架连接到数据库,但指南中有一个步骤引用了一个名为project.json 的文件,其中行:@987654325应该添加@。

问题是该文件在 Visual Studio 2017 项目中不存在,正如 this guide 中所确认的那样,我在我的项目中的任何地方都看不到 .csproj 文件,文章引用该文件作为 project.json 的替代品.

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

【问题讨论】:

标签: c# sql asp.net-mvc visual-studio-2017 asp.net-core-mvc


【解决方案1】:

appsettings.json

"ConnectionStrings": {
    "Cn": "Server=(localdb);Database=TestDB;Persist Security Info=True;User ID=sa;Password=abcd123!"
  }

DbContext

public TestDbContext:DbContext
{
    public TestDbContext(DbContextOptions<TestDbContext> option) : base(option)
    {

    }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
    {
         relationship.DeleteBehavior = DeleteBehavior.Restrict;
    }
    base.OnModelCreating(modelBuilder);
}

public DbSet<Users> Users { get; set; }

Startup.cs

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

最后,您可以在模型类库下使用代码优先的方法。

【讨论】:

  • 需要在哪个文件中创建 db 上下文,仅此而已?
  • @loremIpsum1771 在 Model 文件夹下,您可以创建 TestDbContext。这个教程网站可以帮助你:entityframeworktutorial.net/efcore/entity-framework-core.aspx
  • 感谢您的链接。它提供了有关如何从现有表中派生模型的信息
【解决方案2】:

当我学习如何创建 ASP.NET Core 项目时,我的老师建议我们遵循 MS 文档中的 guide。我建议您按照该指南来解决您的问题。在该指南中说您需要将数据库的连接字符串添加到位于项目主文件夹中的文件appsettings.json。连接字符串是这样的:

"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 方法:

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

然后方法会是这样的:

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 的数据库连接的步骤。对于接下来的步骤,我建议您关注guide

【讨论】:

  • Biblioteca_Context 来自哪里?
  • 它是我做的一个示例练习......对不起,我改变了......
  • 上下文类应该在哪里定义?
  • @loremIpsum1771 Context 应该在项目的另一个源文件中定义。 Context 类继承自 DbContext 类,该类包含在 System.Data.Entity 命名空间中。这是 Microsoft MSDN 上的 DbContext 类定义:msdn.microsoft.com/en-us/library/…
  • 所以我只需要将另一个名为 yourContextClass 的类(在本例中)添加到 VS 项目的根目录中?
猜你喜欢
  • 2020-01-31
  • 2014-10-13
  • 2017-06-07
  • 2013-04-08
  • 2022-11-18
  • 2021-10-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多