Startup.cs ConfigureServices
appsettings.json
problem
- Consider how good name
Troubleshooting
- And LocalContext table name in the same class name
- Add connectionstring in Startup.cs reference service.AddContext
connectionstring in Chinese character data table - A copy of the form of reference data to txt again
- Code First 的 增 改删
Add-Migration Initial
Add-Migration AddModelName
Add-Migration ModifyModelName
Update-Database - Authentication is used
to add authentication cookie information and app.useAuthentication in Startup.cs in ()
to add years to perform at login - Additions to the deletion plus entity
using _context.Student_DB. Add and the Remove
_context.SaveChanges ()
、
- 一些有用的命令
public LocalContext(DbContextOptions<LocalContext> options) : base(options) { }
public DbSet<Student> Students_DB { get; set; }
public DbSet<Course> Courses_DB { get; set; }
"ConnectionStrings": {
"StudentsDb": "Data Source=DESKTOP-61L5TCQ\MSSQLSERVER2017;Initial Catalog=StudentsDb;User ID=sa;Password=123456;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
services.AddDbContext<LocalContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("StudentsDb")));
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = new PathString("/login/");
options.AccessDeniedPath = new PathString("/login/denied");
});
app.UseAuthentication();
Add-Migration Initial
Add-Migration AddModelName
Add-Migration ModifyModelName
string username = Request.HttpContext.User.Claims.FirstOrDefault(s => s.Type == ClaimTypes.Sid).Value;
string role = Request.HttpContext.User.Claims.FirstOrDefault(s => s.Type == ClaimTypes.Role).Value;
ViewBag.Stu = _context.Students_DB.FirstOrDefault(x => x.Username == username);
_context.Students_DB.Add(stu);
_context.SaveChanges();
return RedirectToAction(nameof(MainController.Index),"Main");
Student stu = _context.Students_DB.Find(id);
_context.Students_DB.Remove(stu);
_context.SaveChanges();
return RedirectToAction(nameof(MainController.Index), "Main");
<a href="add"> add </a> and <a href="/add"> add </a> difference
<form action="filter">
<p>
学号: <input type="text" name="identify">
<input type="submit" value="filter" />
</p>
</form>
public IActionResult Filter(int identify)
{
//Student stu = _context.Students_DB.Find(identify);
var stu = _context.Students_DB.Where(x => x.SNumber == identify);
ViewBag.Stu = stu;
return View();
}