编程笔记提要
bootstrap之navbar : https://www.cnblogs.com/jipinglong/p/9032640.html
在页面上可以用@Styles.Render("~/Content/css") 来加载css,首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件
cshtml文件:*.c文件只要传递一个Dictionary,而在cshtml文件不要@ using模型
@foreach (string key in Model.Keys)
{
<tr>
<th>@key</th>
<td>@Model[key]</td>
</tr>
}
ASP.NET Identity
Web.config中:
<add key="owin:AppStartup" value="UsersManagement.IdentityConfig" /> IdentityConfig的名空间
数据库连接字符串
创建 User 类
创建 Database Context 类
创建User Manger 类
创建OWIN Startup 类IdentityConfig
添加验证:
CustomPasswordValidator
CustomUserValidator
用户add\delete
使用Katana进行身份验证UseCookieAuthentication
添加用户并实现身份验证Login(LoginModel model,string returnUrl)
未登录请求重定向
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "MyCookieMiddlewareInstance",
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
Code First 数据库建立及初始化:
namespace EFTest.DAL { public class SchoolContext : DbContext { public SchoolContext() : base("SchoolContext") //数据库连接 { } public DbSet<Student> Students { get; set; } //DbSet<T>定义数据库模型 public DbSet<Enrollment> Enrollments { get; set; } public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) //用来改变一些约束,例如表名不用变复数、改表名、列名等等 { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } }
SchoolInitializer类用来定义数据库在初始化的时候需要做的一些事情;比如通过重载Seed方法预先放入一些数据等
using System; using System.Collections.Generic; using EFTest.Models; namespace EFTest.DAL { public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<SchoolContext> { protected override void Seed(SchoolContext context) { var students = new List<Student> { new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")}, new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")}, new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")}, new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")}, new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")}, new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")} }; students.ForEach(s => context.Students.Add(s)); context.SaveChanges();
var courses = new List<Course> { new Course{CourseID=1050,Title="Chemistry",Credits=3,}, new Course{CourseID=4022,Title="Microeconomics",Credits=3,}, new Course{CourseID=4041,Title="Macroeconomics",Credits=3,}, new Course{CourseID=1045,Title="Calculus",Credits=4,}, new Course{CourseID=3141,Title="Trigonometry",Credits=4,}, new Course{CourseID=2021,Title="Composition",Credits=3,}, new Course{CourseID=2042,Title="Literature",Credits=4,} }; courses.ForEach(s => context.Courses.Add(s)); context.SaveChanges();
var enrollments = new List<Enrollment> { new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A}, new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C}, new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B}, new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B}, new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F}, new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F}, new Enrollment{StudentID=3,CourseID=1050}, new Enrollment{StudentID=4,CourseID=1050,}, new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F}, new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C}, new Enrollment{StudentID=6,CourseID=1045}, new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A}, }; enrollments.ForEach(s => context.Enrollments.Add(s)); context.SaveChanges(); } } }
初始化数据库方法1:
1、在Web.config 中定义:
在<entityFramework>节点中,增加<contexts>节点,分别定义<context>的type ,以及初始化<databaseInitializer>的type: (具体为啥这样定义,只能以后看原理。。。)
<entityFramework> <contexts> <context type="EFTest.DAL.SchoolContext, EFTest"> <databaseInitializer type="EFTest.DAL.SchoolInitializer, EFTest" /> </context> </contexts> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>
这种方法只有在操作数据库读写时才会建立,例如加一个访问代码
using EFTest.DAL; using System.Linq; using System.Web.Mvc; namespace EFTest.Controllers { public class StudentController : Controller { private SchoolContext db = new SchoolContext(); // GET: Student public ActionResult Index() { return View(db.Students.ToList()); } } }
初始化数据库方法2:
2、在Global.asax中执行初始化;在Application_Start()方法中,最后加上以下数据库初始化代码:
Database.SetInitializer<SchoolContext>(new SchoolInitializer());
另外有:
public class EFDbContext : DbContext { public DbSet<User> Users { get; set; } public DbSet<Role> Roles { get; set; } public DbSet<UserRoleRelation> UserRoleRelations { get; set; } public DbSet<UserConfig> UserConfig { get; set; } public EFDbContext() : base("DefaultConnection") { Database.CreateIfNotExists();// 如果数据库不存在则创建 } }