小編給大家分享一下ASP.NET使用EntityFrameworkCore CodeFrist 的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)的服務宗旨!把網站當作互聯(lián)網產品,產品思維更注重全局思維、需求分析和迭代思維,在網站建設中就是為了建設一個不僅審美在線,而且實用性極高的網站。創(chuàng)新互聯(lián)對網站制作、成都網站建設、網站制作、網站開發(fā)、網頁設計、網站優(yōu)化、網絡推廣、探索永無止境。
1,首先根據個人需要下載對應的微軟官方.net core sdk和runtime
2,創(chuàng)建一個asp.net Core的項目
3,創(chuàng)建實體Entity,用戶和角色
public class User { public int Id { get; set; } ////// 角色Id /// public int RoleId { get; set; } public virtual Role Role { get; set; } ////// 狀態(tài) /// public int Status { get; set; } ////// 登陸名 /// public string Login { get; set; } ////// 登陸密碼 /// public string Pwd { get; set; } }
public class Role { public int Id { get; set; } ////// 角色名 /// public string Name { get; set; } ////// 一個角色的多個用戶 /// public virtual ICollectionUsers { get; set; } }
4,創(chuàng)建DbContext
public class EFDbContext: DbContext { public EFDbContext(DbContextOptionsoptions) : base(options) { } public DbSet Roles { get; set; } public DbSet Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { Role(modelBuilder); User(modelBuilder); } private void User(ModelBuilder modelBuilder) { var userBuilder = modelBuilder.Entity ().ToTable("User"); // Properties userBuilder.Property(t => t.Id).ValueGeneratedOnAdd(); userBuilder.Property(t => t.RoleId).IsRequired(); userBuilder.Property(t => t.Status).IsRequired(); userBuilder.Property(t => t.Login).IsRequired().HasMaxLength(30); userBuilder.Property(t => t.Pwd).IsRequired().HasMaxLength(60); // Primary Key userBuilder.HasKey(t => t.Id); // Index userBuilder.HasIndex(t => t.Login); // Relationships userBuilder.HasOne(t => t.Role).WithMany(t => t.Users).HasForeignKey(t => t.RoleId); } private void Role(ModelBuilder modelBuilder) { var roleBuilder = modelBuilder.Entity ().ToTable("Role"); // Properties roleBuilder.Property(t => t.Id).ValueGeneratedOnAdd(); roleBuilder.Property(t => t.Name).IsRequired().HasMaxLength(30); // Primary Key roleBuilder.HasKey(t => t.Id); } }
5,創(chuàng)建DbInitializer,用于創(chuàng)建初始數(shù)據
public class DbInitializer { public async static Task InitData(EFDbContext context) { if (context.Database != null && context.Database.EnsureCreated()) { //角色配置 context.Roles.AddRange(new Role[] { new Role { Name="超級管理員" }, new Role { Name="管理員" } }); //默認用戶 context.Users.AddRange(new User[] { new User { RoleId=1, Login="administrator", Pwd="111111" }, new User { RoleId=2, Login="admin", Pwd="111111" } }); await context.SaveChangesAsync(); } }
6,修改Startup類方法
在ConfigureServices添加如下代碼
// DbContext services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
在Configure方法內添加
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); await DbInitializer.InitData(app.ApplicationServices.GetService()); }
7,修改appsettings.json
{ "ConnectionStrings": { "DefaultConnection": "Data Source=.;Initial Catalog=LniceCore;Integrated Security=SSPI;" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } }
8,運行程序,查看結果
看完了這篇文章,相信你對ASP.NET使用EntityFrameworkCore CodeFrist 的方法有了一定的了解,想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!