先贴上解决方案截图

一、新建4个解决方案文件夹

1-Presentation

2-Application

3-Domain

4-Infrastructure

二、在解决方案文件夹中分别创建项目

其余项目创建省略

项目引用关系:

1.ContosoUniversity.WebAdmin引用ContosoUniversity.Application、ContosoUniversity.Domain

2.ContosoUniversity.Application引用ContosoUniversity.Repository、ContosoUniversity.Domain

3.ContosoUniversity.Repository引用ContosoUniversity.Domain

4.ContosoUniversity.Domain不引用任何项目

三、ContosoUniversity.Domain项目中添加dll Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.Relational

四、ContosoUniversity.Domain项目添加Student、SchoolContext、DbInitializer类

Student:POCO对象,对应数据库中的Student表

SchoolContext:数据库上下文,用于数据库CRUD以及Migrations操作

DbInitializer:初始化数据库并添加测试数据

using System;

namespace ContosoUniversity.Domain
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
}
using Microsoft.EntityFrameworkCore;

namespace ContosoUniversity.Domain.Data
{
public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("Student");
}
}
}
using System;
using System.Linq; namespace ContosoUniversity.Domain.Data
{
public static class DbInitializer
{
public static void Initialize(SchoolContext context)
{
context.Database.EnsureCreated(); // Look for any students.
if (context.Students.Any())
{
return; // DB has been seeded
} var students = new Student[]
{
new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
};
foreach (Student s in students)
{
context.Students.Add(s);
}
context.SaveChanges();
}
}
}

五、ContosoUniversity.WebAdmin项目修改

1.appsetting.json文件添加MySQL连接字符串

 "ConnectionStrings": {
"DefaultConnection": "server=xxx;user id=xxx;password=xxx;database=ContosoUniversity;"
}

2.添加NuGet包MySql.Data.EntityFrameworkCore 6.10.0-alpha、Microsoft.EntityFrameworkCore.Tools 1.1.0-preview4-final

打开工程.csproj工程文件,添加CliTool“Microsoft.EntityFrameworkCore.Tools.DotNet”

否则在migrations操作时会报【No executable found matching command "dotnet-ef"】错误

MySql版本不要选7.0.6-IR31,项目跑起来会报"MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLCommandBuilderFactory..ctor(ISensitiveDataLogger<RelationalCommandBuilderFactory> logger, DiagnosticSource diagnosticSource, IRelationalTypeMapper typeMapper)"错误

3.StartUp类ConfigureServices方法注入数据库上下文

      public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SchoolContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("ContosoUniversity.WebAdmin"))); // Add framework services.
services.AddMvc();
}

注意,标红的代码不可缺少,否则EntityFramework无法执行Migrations,报错信息如下

4.StartUp添加数据库初始化

改造Configure方法签名,添加SchoolContext参数

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SchoolContext context)

Configure方法末尾添加数据库初始化代码

DbInitializer.Initialize(context);

最后

把其余各层的代码都加上项目就可以跑起来了,通过Migrations操作维护开发库,.NET Core+MySQL+EF使用VS2017RC构建项目的坑基本就是这些了。。

注意

NuGet包Install或Uninstall命名执行后,查看VS2017RC中依赖的NuGet包发现没有变化(实际上已Install或Uninstall,VS2017RC没有刷新),此时需要关闭解决方案重新打开,这时NuGet依赖才会刷新,这时VS2017RC的一个BUG!

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序的更多相关文章

  1. VS+mysql+EF搭建

    2016年7月6日更新: vs2010只需要安装mysql的.net connector就可以 vs2012, vs2015都需要安装.net connector + ODBC connector才行 ...

  2. Django + mysql 快速搭建简单web投票系统

    了解学习pyhton web的简单demo 1. 安装Django, 安装pyhton 自行百度 2. 执行命令创建project  django-admin.py startproject mysi ...

  3. 十二个 ASP.NET Core 例子——1.1版本 EF MySql快速搭建

    core1.0的时候搭建过一次mysql EF. 一大推问题.最近在core1.1 又重新搭了一次.简单搭建还挺快,没出现什么幺蛾子.总结下步骤 建立项目,例如ASP.NET Core1.1 WebA ...

  4. 一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建

    作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把,发现速度确实是快了很多,其中也遇到一些小问题,所以整理了一些学习笔记: 阅读目录 环境说明 安装C ...

  5. Linux CentOS 安装MySql以及搭建MySql主从复制

    前言 在之前的博客中,有过几篇都写了关于mysql在linux下的搭建教程,可能以后还会再写,但是又不想重复在写, 于是便想单独将此抽出来,单独写成一篇博客,并详细记录一些安装过程以及遇到的问题解决办 ...

  6. Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程

    原文地址:http://www.osyunwei.com/archives/7378.html 搬运是为了自己找资料方便. 准备篇 一.环境说明: 操作系统:Windows Server 2012 R ...

  7. django+nginx+xshell简易日志查询,接上<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>

    纠正一下之前在<关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思>中说到的PHP+MySQL太慢,这里只是说我技术不好,没 ...

  8. 关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思

    关于<rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>>的反思--链接--http://www.cnblogs.com/drgcaosheng/p/ ...

  9. Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程

    准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: htt ...

随机推荐

  1. MySQL8.0的安装与配置(Windows 10)

    MySQL的下载 进入MySQL官网 https://www.mysql.com/ 选择 DOWNLOAD 到页面底部找到 MySQL Community Edition (GPL) 找到MySQL ...

  2. tomcat 配置域名访问应用

    <Host appBase="webapps" autoDeploy="true" name="www.XXX.com" unpack ...

  3. Sophus链接错误

    错误指示如下: CMakeFiles/run_vo.dir/run_vo.cpp.o: In function `main': run_vo.cpp:(.text.startup+0x1086): u ...

  4. 解决maven update project 后项目jdk变成1.5的问题

    一.问题描述 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.7, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5. 二.原因分析 ...

  5. 使用UIkit的uk-form-icon后input框无法输入的问题

    相关版本UIkit2.27.5 uikit.min.css默认使用uk-form-icon的属性pointer-events: none:因此表框无法点击. <style type=text/c ...

  6. JDBC连接MySql,配置url报错

    使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one ...

  7. Alpha 冲刺 (3/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助后端界面的开发 搭建项目运行的服务器环境 ...

  8. spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found

    解决方法,在repository加一个注解.如下图所示: @Component

  9. swift -inout关键字

    一般参数仅仅是在函数内可以改变的,当这个函数执行完后变量就会被销毁,不会有机会改变函数以外的变量,那么我们就会产生一个疑问,我们可不可以通过一个函数改变函数外面变量的值呢?答案是肯定的,这时我们就需要 ...

  10. Mat补充

    Mat的创建 1.使用Mat的构造函数 Mat test(2,2,CV_8UC3,Scalar(0,0,255)); 2.使用Mat的构造函数 int sizes[3] = {2,2,2}; Mat ...