博客
关于我
.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序
阅读量:798 次
发布时间:2023-04-16

本文共 3810 字,大约阅读时间需要 12 分钟。

.NET Core + MySQL + Entity Framework Core 在 Visual Studio 2017 RC 中的项目搭建与配置

在搭建 .NET Core 项目时,特别是结合 MySQL 数据库和 Entity Framework Core 的使用,需要进行一系列配置和优化。以下是基于 Visual Studio 2017 RC 的详细步骤说明。

一、项目文件夹结构

首先,需要创建一个清晰的项目文件夹结构,按照功能层次划分。具体分为以下四个解决方案文件夹:

  • Presentation:用于 UI 层面的项目,如 ASP.NET Core 应用程序。
  • Application:负责业务逻辑的实现。
  • Domain:定义数据模型和核心逻辑。
  • Infrastructure:负责基础设施配置,包括数据库和依赖组件。
  • 二、解决方案文件夹中的项目创建

    在每个解决方案文件夹中,需要创建对应的项目:

    • ContosoUniversity.WebAdmin:作为前端应用程序,负责展示和交互。
    • ContosoUniversity.Application:作为业务逻辑层,负责服务和逻辑处理。
    • ContosoUniversity.Repository:作为数据访问层,负责数据库交互。
    • ContosoUniversity.Domain:作为数据模型和上下文层。

    项目之间的引用关系需要注意:

    • ContosoUniversity.WebAdmin 依赖于 ContosoUniversity.ApplicationContosoUniversity.Domain
    • ContosoUniversity.Application 依赖于 ContosoUniversity.RepositoryContosoUniversity.Domain
    • ContosoUniversity.Repository 依赖于 ContosoUniversity.Domain
    • ContosoUniversity.Domain 不直接依赖任何项目。

    三、数据库与 Entity Framework 配置

    ContosoUniversity.Domain 项目中,需要添加必要的 NuGet 包:

    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.Relational

    此外,还需要添加以下核心组件:

    • Student:对应数据库中的 Student 表,作为实体类。
    • SchoolContext:作为数据库上下文,用于执行 CRUD 操作和 migrations。
    • DbInitializer:用于初始化数据库,并添加测试数据。
    using System;using Microsoft.EntityFrameworkCore;namespace ContosoUniversity.Domain.Data{    public class SchoolContext : DbContext    {        public SchoolContext(DbContextOptions
    options) : base(options) { } public DbSet
    Students { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity
    ().ToTable("Student"); } }}
    using System;using System.Linq;namespace ContosoUniversity.Domain.Data{    public static class DbInitializer    {        public static void Initialize(SchoolContext context)        {            context.Database.EnsureCreated();            if (context.Students.Any())            {                return;            }            var students = new Student[]            {                new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01") },                // 其他测试数据            };            foreach (Student s in students)            {                context.Students.Add(s);            }            context.SaveChanges();        }    }}

    四、WebAdmin 项目配置

    ContosoUniversity.WebAdmin 项目中,需要进行以下配置:

  • 添加 MySQL 连接字符串:在 appsettings.json 文件中配置数据库连接。
  • "ConnectionStrings": {    "DefaultConnection": "server=xxx;user id=xxx;password=xxx;database=ContosoUniversity;"}
    1. 安装必要的 NuGet 包
      • MySql.Data.EntityFrameworkCore 6.10.0-alpha
      • Microsoft.EntityFrameworkCore.Tools 1.1.0-preview4-final
      1. 配置项目文件:在 .csproj 文件中添加 CLI 工具:
      2. netcoreapp2.0
        2.0.0-rc.1-03158f9
        false
        1. 配置数据库上下文:在 Startup.cs 中的 ConfigureServices 方法中添加数据库配置。
        2. public void ConfigureServices(IServiceCollection services){    services.AddDbContext
          ( options => options.UseMySQL( Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("ContosoUniversity.WebAdmin") ) ); services.AddMvc();}
          1. 数据库初始化:在 Configure 方法中添加初始化代码。
          2. public void Configure(    IApplicationBuilder app,    IHostingEnvironment env,    ILoggerFactory loggerFactory,    SchoolContext context){    // 其他配置代码...    DbInitializer.Initialize(context);}

            五、完整项目运行准备

          3. 项目引用关系:确保每个项目正确引用所需的项目和 NuGet 包。
          4. Migrations 操作:通过 dotnet ef 命令进行 migrations,确保数据库状态一致。
          5. 测试环境配置:确保 MySQL 服务已正确配置,防止因版本不兼容导致错误。
          6. 注意事项

            • 依赖管理:在安装或卸载 NuGet 包后,确保重启 Visual Studio,以获取最新的依赖项。
            • MySQL 版本:请避免使用 MySQL 7.0.6-IR31,否则会出现存储引擎相关错误。
            • 迁移配置:确保迁移工具路径正确,避免错误信息如 "No executable found matching command 'dotnet-ef'"。

            通过以上步骤,可以在 Visual Studio 2017 RC 中成功搭建并配置 .NET Core 项目,结合 MySQL 数据库和 Entity Framework Core 进行开发。

    转载地址:http://odgfk.baihongyu.com/

    你可能感兴趣的文章
    Mysql-事务阻塞
    查看>>
    Mysql-存储引擎
    查看>>
    mysql-开启慢查询&所有操作记录日志
    查看>>
    MySQL-数据目录
    查看>>
    MySQL-数据页的结构
    查看>>
    MySQL-架构篇
    查看>>
    MySQL-索引的分类(聚簇索引、二级索引、联合索引)
    查看>>
    Mysql-触发器及创建触发器失败原因
    查看>>
    MySQL-连接
    查看>>
    mysql-递归查询(二)
    查看>>
    MySQL5.1安装
    查看>>
    mysql5.5和5.6版本间的坑
    查看>>
    mysql5.5最简安装教程
    查看>>
    mysql5.6 TIME,DATETIME,TIMESTAMP
    查看>>
    mysql5.6.21重置数据库的root密码
    查看>>
    Mysql5.6主从复制-基于binlog
    查看>>
    MySQL5.6忘记root密码(win平台)
    查看>>
    MySQL5.6的Linux安装shell脚本之二进制安装(一)
    查看>>
    MySQL5.6的zip包安装教程
    查看>>
    mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
    查看>>