//查看EFCore命令:get-help entityframeworkcore
//script-migration 生成sql脚本
//Model里有一个省份Province,一个城市City
//在 DBContex 里:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//添加了一个省份的种子,Id是自增的
modelBuilder.Entity<Province>().HasData(
new Province
{
Id = 1,
Name="广东",
Populiation=70000000
});
}
生成迁移:
Add-Migration xxx
获取客户端IP地址
在Startup.cs里:
services.AddHttpContextAccessor();
service.TryAddSingleton<IActionContextAccessor,ActionContextAccessor>();
在MVC视图里:
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
使用方法:
Client IP: @HttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString()
Session 和 Cookie
1. 手动添加Session支持:
services.AddDistributedMemoryCache();
services.AddSession(options=>
{
options.IdleTimeOut = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
});
2. app.UseSession();
3. 设定和获取Session值:
HttpContest.Session.SetString("CaptchaCode",resultCaptchaCode);
HttpContext.Session.GetString("CaptchaCode");
清除值:
context.Session.Remove("CaptchaCode");
近期评论