- [檔案]->[新增]->[專案]
- Visual C#->Web->ASP.NET Web 應用程式(.NET Framework)
- 名稱:
- 方案名稱:
- 選 MVC
加入會用到的 NuGet 模組
- Install-Package WebMatrix.Data (會安裝 Microsoft.AspNet.WebPages.Data 及 WebMatrix.Data)
加入資料庫通用模組
ConnDB
★using WebMatrix.Data;
把資料庫,反向工程,轉成XXXXXX.cs Models 函式
VS2017
工具>NgGet封裝管理員>套件管理器主控台
執行下列命令 (""裡的字串,是複製資料表>屬性>連接字串)
PM>scaffold-dbcontext "Data Source=資料庫IP;Initial Catalog=資料表;Integrated Security=False;User ID=使用者;Password=密碼;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer -force
會轉出如下資料
using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; namespace 公告推撥系統 { public partial class NTTU_BulletinContext : DbContext { public virtual DbSet<MainBulletin> MainBulletin { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings. optionsBuilder.UseSqlServer(@"Data Source=xxx.xxx.xxx.xxx;Initial Catalog=資料表;Integrated Security=False;User ID=帳號;Password=密碼;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<MainBulletin>(entity => { entity.HasKey(e => e.BulletinNumber) .HasName("PK_main_bulletin"); entity.ToTable("main_bulletin"); entity.Property(e => e.BulletinNumber) .HasColumnName("bulletin_number") .ValueGeneratedNever(); entity.Property(e => e.ActivityPost).HasColumnName("activity_post"); entity.Property(e => e.BulletinContent) .HasColumnName("bulletin_content") .HasColumnType("varchar(max)"); entity.Property(e => e.BulletinTitle) .HasColumnName("bulletin_title") .HasColumnType("varchar(max)"); entity.Property(e => e.DraftBoolean).HasColumnName("draft_boolean"); entity.Property(e => e.FilePath) .HasColumnName("file_path") .HasColumnType("varchar(250)"); entity.Property(e => e.MeetingPost).HasColumnName("meeting_post"); entity.Property(e => e.OpenPost).HasColumnName("open_post"); entity.Property(e => e.PostDateTime) .HasColumnName("post_date_time") .HasColumnType("smalldatetime"); entity.Property(e => e.PostEmail) .HasColumnName("post_email") .HasColumnType("varchar(100)"); entity.Property(e => e.PostRemind).HasColumnName("post_remind"); entity.Property(e => e.PostUnit) .IsRequired() .HasColumnName("post_unit") .HasColumnType("varchar(100)"); entity.Property(e => e.StartDateTime) .HasColumnName("start_date_time") .HasColumnType("smalldatetime"); entity.Property(e => e.StopDateTime) .HasColumnName("stop_date_time") .HasColumnType("smalldatetime"); }); } } }