ASP.NET使用EntityFrameworkCoreCodeFrist的方法
小编给大家分享一下ASP.NET使用EntityFrameworkCore CodeFrist 的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
“只有客户发展了,才有我们的生存与发展!”这是创新互联的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对网站制作、成都网站建设、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。
1,首先根据个人需要下载对应的微软官方.net core sdk和runtime
2,创建一个asp.net Core的项目
3,创建实体Entity,用户和角色
public class User { public int Id { get; set; } ////// 角色Id /// public int RoleId { get; set; } public virtual Role Role { get; set; } ////// 状态 /// 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,创建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,创建DbInitializer,用于创建初始数据
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 的方法有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
当前名称:ASP.NET使用EntityFrameworkCoreCodeFrist的方法
当前地址:http://lswzjz.com/article/ijshio.html