玩了一天,参考了Adam Freeman的书,总结一下,以备后来查看。
1、创建一个ASP.NET Web Application ——MVC 选择空模板
MVC 三个文件夹功能很清晰 Models Views Controllers
2、使用NuGet安装bootstrap,EntityFramework,Ninject......
NuGet很好用,可以可视化操作,比maven方便,下载速度也很快。
bootstrap 用于前端 ,EF是.net的ORM(对象关系映射),Ninject是一个依赖注入DI容器
3、体验EF的Code-First
A 创建实体类
这里比较下Entity和Model
Entity 和数据库对应的实体类
Model是MVC中一个概念,可能不和Entity一一对应,因为展示在View层中数据可能是一个Entity的精简,也可能是多个Entity的组合。一句话概括:Model是一个高度优化组合或者精简后的一个用于在View层展示数据的对象。
Model可以是多个Entity的集合
using System; namespace WXPlatform.Entities { public class Student { public int Id { get; set; } public string StudentId { get; set; } public string ClassId { get; set; } public string CollegeId { get; set; } public string IdentityId { get; set; } public string LoginName { get; set; } public string RealName { get; set; } public string NickName { get; set; } public string Password { get; set; } public string PhoneNumber { get; set; } public DateTime LastVisit { get; set; } public int VisitCount { get; set; } public string Address { get; set; } public string Authority { get; set; } } }B 创建上下文 EFDbContext.cs 这个文件用以关联Entity和DB
using System.Data.Entity; using WXPlatform.Entities; namespace WXPlatform.Concrete { public class EFDbContext : DbContext { public DbSet<Student> Student { get; set; } } }
C Web.config
注意这里的name和上面的上下文的name
<connectionStrings> <add name="EFDbContext" connectionString="Data Source=*;Initial Catalog=*;User ID=*;Password=*;" providerName="System.Data.SqlClient" /> </connectionStrings>D 接口来一个
书上把抽象都放在Abstract文件夹,把实现都放在Concrete文件夹
using System.Collections.Generic; using WXPlatform.Entities; namespace WXPlatform.Abstract { public interface IStudentRepository { IEnumerable<Student> Student { get; } } }E 创建存储库类
using System.Collections.Generic; using WXPlatform.Entities; using WXPlatform.Abstract; namespace WXPlatform.Concrete { public class EFStudentRepository : IStudentRepository { private EFDbContext context = new EFDbContext(); public IEnumerable<Student> Student { get { return context.Student; } } } }F 程序包管理器控制台 指令Enable-Migrations 会生成 Migrations\Configuration.cs
里面有一句改自动true AutomaticMigrationsEnabled = true;
数据库中会生成一个__MigrationHistory表和Entity对应的数据库,表的名字加s,比如Student表名为Students
Enable-Migrations还有个强制参数 -force
如果数据库结构被修改报错了怎么办?---------------直接删掉__MigrationHistory表
Code-First
所有string生成的都是 NVARCHAR (MAX) NULL
DateTime 类型 DATETIME NOT NULL,
INT 类型 NOT NULL,
int Id 会被转为 INT IDENTITY (1, 1) NOT NULL,
都可以修改的
4、Ninject
A NinjectDependencyResolver.cs
关键绑定的句子:kernel.Bind<IStudentRepository>().To<EFStudentRepository>();
书上也有模仿存储库的例子,用的是Moq库
Mock<IStudentRepository> mock=new Mock<IStudentRepository>();
mock.Setup(m=>m.Students).Returns(new List<Student>{
new Student {RealName="",NickName=""....... },
new Student {RealName="",NickName=""....... }
});
using System; using System.Collections.Generic; using System.Web.Mvc; using Ninject; using WXPlatform.Abstract; using WXPlatform.Concrete; namespace WXPlatform.Infrastructure { public class NinjectDependencyResolver : IDependencyResolver { private IKernel kernel; public NinjectDependencyResolver(IKernel kernelParam) { kernel = kernelParam; AddBindings(); } public object GetService(Type serviceType) { return kernel.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return kernel.GetAll(serviceType); } private void AddBindings() { kernel.Bind<IStudentRepository>().To<EFStudentRepository>(); } } }B NuGet装了
NinjectNinject.Web.CommonNinject.Web.Mvc居然App_Start里没有 Ninject.Web.Common.cs文件
后来补装了Ninject.Web.WebApi.WebHost 有了Ninject.Web.Common.cs里
/// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { System.Web.Mvc.DependencyResolver.SetResolver(new WXPlatform.Infrastructure.NinjectDependencyResolver(kernel)); }5、Controller & View
HomeController.cs
using System.Web.Mvc; using WXPlatform.Abstract; namespace WXPlatform.Controllers { public class HomeController : Controller { private IGradeRepository repository; public HomeController(IGradeRepository GradeRepository) { this.repository = GradeRepository; } public ViewResult Index() { return View(repository.Grade); } } }Index.cshtml
@using WXPlatform.Entities; @model IEnumerable<Student> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> @foreach (var p in Model) { <div> <h1>@p.RealName</h1> </div> } </body> </html>