標籤:entityframework ef asp.net core code first
建立asp.net core項目,然後把.net core引用的類庫升級到1.1,這時,需要手動在project.json下添加一個runtimes節點,如下:
"runtimes": {
"win10-x64": {}
}
同時在Nuget中添加下面三個類庫(1.1版本):
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
接下來定義DbContext,用來產生資料庫,代碼如下:
using Microsoft.EntityFrameworkCore;using System.Collections.Generic;using System.ComponentModel.DataAnnotations; namespace EntityFrameworkDemo.Model{ /// <summary> /// 資料庫物件 /// </summary> public class PermissionContext : DbContext { public PermissionContext(DbContextOptions<PermissionContext> opt) : base(opt) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { //確定UserRole表中的兩個欄位是聯合主鍵 modelBuilder.Entity<UserRole>() .HasKey(u=>new { u.UserID,u.RoleID}); } public DbSet<User> Users { get; set; } public DbSet<Role> Roles { get; set; } public DbSet<UserRole> UserRoles { get; set; } } /// <summary> /// 使用者表 /// </summary> public class User { [Key] public int ID { get; set; } public string UserName { get; set; } public string Password { get; set; } public List<UserRole> UserRoles { get; set; } } /// <summary> /// 角色表 /// </summary> public class Role { [Key] public int ID { get; set; } public string RoleName { get;set; } public List<UserRole> UserRoles { get; set; } } /// <summary> /// 使用者角色關係 /// </summary> public class UserRole { public int UserID { get; set; } public int RoleID { get; set; } public User User{ get; set; } public Role Role { get; set; } }}
這時,需要在StartUp.cs中添加資料連字號串,來指導自動產生資料庫時的伺服器,資料庫名等資訊
public void ConfigureServices(IServiceCollection services){ var connection = @"Server=.;Database=PermissionDb;Trusted_Connection=True;"; services.AddDbContext<PermissionContext>(options => options.UseSqlServer(connection)); services.AddMvc();}
現在,先Build一下項目,用兩個命令在封裝管理員控制台(vs的菜單“工具”-“NuGet包管理器”-“封裝管理員控制台”)中執行:
Add-Migration MyFirstMigration
用來產生命令,產生資料庫和表的C#代碼
Update-Database
執行產生的程式碼
在用Add-Migration MyFirstMigration時會報個錯,在netcoreapp1.1中沒有需要的項目設定檔(.json的),這時開啟bin目錄,會發現在netcoreapp1.1下會多一個win10-x64檔案夾,這正是我們手工在project.json中添加的,開啟這個檔案夾,把裡面對應的.json複製出來就可以(這裡應該是Add-MigrationMyFirstMigration產生代碼時,預設的尋找設定檔與我們手工添加runtimes的路徑不一直導致)
650) this.width=650;" title="11.png" alt="wKiom1iKmvaTsjQTAADatm1hv7I382.png-wh_50" src="http://s2.51cto.com/wyfs02/M01/8D/22/wKiom1iKmvaTsjQTAADatm1hv7I382.png-wh_500x0-wm_3-wmp_4-s_4018575647.png" />
複製完後再Add-Migration MyFirstMigration一次
會發現在項目中添加了一個檔案夾Migrations,並在下面產生兩個檔案,這便是產生資料庫所需的指令。
現在再執行Update-Database
650) this.width=650;" title="12.png" alt="wKioL1iKmwvAPyhgAACmtzGMgtU688.png-wh_50" src="http://s5.51cto.com/wyfs02/M02/8D/20/wKioL1iKmwvAPyhgAACmtzGMgtU688.png-wh_500x0-wm_3-wmp_4-s_3669587638.png" />
當執行成功後,用SQL Server的管理工具查看,產生的資料庫,並且表中的表關係入下:
650) this.width=650;" title="13.png" alt="wKioL1iKmxeQbjzBAABS1UJXUBo102.png-wh_50" src="http://s3.51cto.com/wyfs02/M01/8D/20/wKioL1iKmxeQbjzBAABS1UJXUBo102.png-wh_500x0-wm_3-wmp_4-s_3931696857.png" />
本文出自 “桂素偉” 部落格,請務必保留此出處http://axzxs.blog.51cto.com/730810/1894317
.net core 1.1下的EntityFramework Code First