先貼上解決方案截圖 一、新建4個解決方案文件夾 1-Presentation 2-Application 3-Domain 4-Infrastructure 二、在解決方案文件夾中分別創建項目 新建.NET Core Web應用程式,【身份驗證】需要選擇【個人用戶賬戶】否則無法執行migration ...
先貼上解決方案截圖
一、新建4個解決方案文件夾
1-Presentation
2-Application
3-Domain
4-Infrastructure
二、在解決方案文件夾中分別創建項目
新建.NET Core Web應用程式,【身份驗證】需要選擇【個人用戶賬戶】否則無法執行migrations操作,猜想原因可能少了某個NuGet包,具體沒去測試驗證
如果不選【個人用戶賬戶】,migrations操作時會報【No executable found matching command "dotnet-ef"】錯誤
其餘項目創建省略
項目引用關係:
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
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!