/// <summary>
/// 系统应用的用户实体
/// </summary>
public class ApplicationUser : BaseModel
{
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 邮件地址
/// </summary>
public string Email { get; set; }
}
/// <summary>
/// 系统上下文
/// </summary>
public class LightContext : DbContext
{
public LightContext(DbContextOptions<LightContext> options) : base(options)
{
}
/// <summary>
/// 系统应用用户
/// </summary>
public DbSet<ApplicationUser> ApplicationUser { get; set; }
/// <summary>
/// 角色表
/// </summary>
public DbSet<Role> Role { get; set; }
}
Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.Tools
| 迁移命令描述 | CMD命令 | PMC命令 |
| 创建迁移:migrationname为迁移名称 | dotnet ef migrations add migrationname | add-migration migrationname |
| 移除迁移(删除最近的一次迁移) | dotnet ef migrations remove | remove-migration |
| 应用最新的迁移(使迁移文件应用到数据库) | dotnet ef database update | update-database |
| 应用指定的迁移 | dotnet ef database update migrationname | update-database migrationname |
| 查看迁移列表 | dotnet ef migrations list | |
| 查看数据库上下文信息 | dotnet ef dbcontext info |
dotnet ef
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" /> </ItemGroup>
dotnet ef migrations add InitLightDB
<PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion> </PropertyGroup>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<LightContext>
{
public LightContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<LightContext>();
builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Initial Catalog=Light;");
return new LightContext(builder.Options);
}
}
dotnet ef database update
public static void ConfigApplicationUser(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>(m =>
{
m.Property(t => t.Email)
.HasMaxLength(50);
m.Property(t => t.UserName)
.IsRequired()
.HasMaxLength(50);
m.Property(t => t.Password)
.IsRequired()
.HasMaxLength(20);
});
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<LightContext>
{
public LightContext CreateDbContext(string[] args)
{
Directory.SetCurrentDirectory("..");//设置当前路径为当前解决方案的路径
string appSettingBasePath = Directory.GetCurrentDirectory() + "/Light.AuthorityApi";//改成你的appsettings.json所在的项目名称
var configBuilder = new ConfigurationBuilder()
.SetBasePath(appSettingBasePath)
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<LightContext>();
//builder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Initial Catalog=Light;");
builder.UseSqlServer(configBuilder.GetConnectionString("LightConnection"));
return new LightContext(builder.Options);
}
}
Install-Package Microsoft.Extensions.Configuration.Json
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有