先贴上解决方案截图

一、新建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:初始化数据库并添加测试数据

  1. using System;
  2.  
  3. namespace ContosoUniversity.Domain
  4. {
  5. public class Student
  6. {
  7. public int ID { get; set; }
  8. public string LastName { get; set; }
  9. public string FirstMidName { get; set; }
  10. public DateTime EnrollmentDate { get; set; }
  11. }
  12. }
  1. using Microsoft.EntityFrameworkCore;
  2.  
  3. namespace ContosoUniversity.Domain.Data
  4. {
  5. public class SchoolContext : DbContext
  6. {
  7. public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
  8. {
  9. }
  10.  
  11. public DbSet<Student> Students { get; set; }
  12.  
  13. protected override void OnModelCreating(ModelBuilder modelBuilder)
  14. {
  15. modelBuilder.Entity<Student>().ToTable("Student");
  16. }
  17. }
  18. }
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ContosoUniversity.Domain.Data
  5. {
  6. public static class DbInitializer
  7. {
  8. public static void Initialize(SchoolContext context)
  9. {
  10. context.Database.EnsureCreated();
  11.  
  12. // Look for any students.
  13. if (context.Students.Any())
  14. {
  15. return; // DB has been seeded
  16. }
  17.  
  18. var students = new Student[]
  19. {
  20. new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
  21. new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
  22. new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
  23. new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
  24. new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
  25. new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
  26. new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
  27. new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
  28. };
  29. foreach (Student s in students)
  30. {
  31. context.Students.Add(s);
  32. }
  33. context.SaveChanges();
  34. }
  35. }
  36. }

五、ContosoUniversity.WebAdmin项目修改

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

  1. "ConnectionStrings": {
  2. "DefaultConnection": "server=xxx;user id=xxx;password=xxx;database=ContosoUniversity;"
  3. }

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方法注入数据库上下文

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddDbContext<SchoolContext>(options =>
  4. options.UseMySQL(Configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("ContosoUniversity.WebAdmin")));
  5.  
  6. // Add framework services.
  7. services.AddMvc();
  8. }

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

4.StartUp添加数据库初始化

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

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

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

  1. 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. $(QTDIR);$(QTDIR)\include\QtCore;$(QTDIR)\include;

    $(QTDIR); 在系统环境变量中定义即可  vs属性中设置头文件路径

  2. Servlet会话管理二(Cookie)

    Cookie是在HTTP协议下,将服务器传递给浏览器的的少量信息保存到浏览器客户端的一种技术,通过这种技术,即使在浏览器被关闭或链接中断的情况下,用户仍可以维护Cookie中的数据. Cookie是经 ...

  3. MySQL中的联结表

    使用联结能够实现用一条SELECT语句检索出存储在多个表中的数据.联结是一种机制,用来在一条SELECT语句中关联表,不是物理实体,其在实际的数据库表中并不存在,DBMS会根据需要建立联结,且会在查询 ...

  4. vs2017控制python版本

    在python环境下拉菜单里面任意一个点右键,选择打开此处的命令提示符,就能把cmd开启的python版本切换到这个版本.

  5. echart 3 数据密集时,断点不显示问题

    如上图最开始标识的两点是不显示的,配置处理代码如下(series中配置symbolSize: 1,showAllSymbol: true): myChart.showLoading(); $.get( ...

  6. CSS-尺寸与边框

    1.基础选择器的优先级 权值:标识当前选择器的重要程度,权值越大优先级越高. 元素选择器 1 类选择器 10 伪类选择器 10 ID选择器 100 内联样式 1000 选择器的权值加到一起,大的优先 ...

  7. kbmmw 中简单返回 extjs 数据JSON

    以前,我们通过自己写json 来返回数据表的内容.在delphi 10.2.2中,官方自带了一个FDBatchMoveJSONWriter1 来直接处理数据库内容.把结果推送到浏览器客户端. 今天我们 ...

  8. 注解Demo

    1.创建注解类 2.在测试实体类中使用注解 3.构建注解解析类 4.执行注解解析方法

  9. Winform自定义表单(转)

    出处:http://www.newlifex.com/showtopic-167.aspx 好吧,附件真的损坏了,原始代码我也没有了,再提取我也没精力了,不好意思,哪位之前下过可以重发一遍吗?不过即使 ...

  10. Win7 VS2013环境cuda_7.5.18的一些坑

    thrust库的sort算法,在x86平台使用就崩溃,x64就没问题,搜了下好像是很早的版本,4开始就有这样的问题了,原因不明. http://stackoverflow.com/questions/ ...