.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序,先贴上解决方案截图

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

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

1-Presentation

2-Application

3-Domain

4-Infrastructure

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

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

新建.NET Core Web应用程序,【身份验证】需要选择【个人用户账户】否则无法执行migrations操作,猜想原因可能少了某个NuGet包,具体没去测试验证

如果不选【个人用户账户】,migrations操作时会报【No executable found matching command "dotnet-ef"】错误

.NET Core1.1+VS2017RC+MySQL+EF搭建多层Web应用程序

其余项目创建省略

项目引用关系:

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

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

3.ContosoUniversity.Repository引用ContosoUniversity.Domain

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

三、ContosoUniversity.Domain项目中添加Microsoft.EntityFrameworkCore.Tools 1.1.0-preview4-final

NuGet命令:Install-Package Microsoft.EntityFrameworkCore.Tools -Pre

四、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 6.10.0-alpha、MySql.Data.EntityFrameworkCore 6.10.0-alpha、Microsoft.EntityFrameworkCore.Tools 1.1.0-preview4-final

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/079046c90ec38276646d2253aac03ad9.html