【.net framework 学习】startUp类源码解析

startUp类和global.asax.cs类

上下文OwinContext。

上下文是连接两个对象之间的操作的类。

app.CreatePerOwinContext(ApplicationDbContext.Create);//在这里就创建数据库上下文。

看下图Create()方法,里面返回了ApplicationDbContext的实例(见下一小节的create方法)

Owin让.net可以部署在其他服务器上。这个OwinContext上下文就是连接.net程序和Owin之间的上下文。类似与DbContext就是连接.net程序和数据库的上下文。

 

上下文ApplicationDbContext(identityModel.cs)

上下文里面的create方法,实例化ApplicateionDbContext,这是一种设计模式。

可以看到里面有Dbset之类的属性,是不是很熟悉?

ApplicationDbContext上下文类:

 

上下文IdentityDbContext<TUser> 身份认证组件的上下文

 

IdentityDbContext<TUser>上下文继承自IdentityDbContext<TUser,IdentityRole,...>这个上下文

​​​​​​​上下文IdentityDbContext<TUser,TRole,...>

​​​​​​​实体类IdentityUserClaim<TKey>,where约束

可以看到UserLogin等和左边数据库是一一对应的

打开IdentityUserClaim,数据表的字段也是一一对应的。

 

IdentityUser类 where约束

IdentityDbContext<TUser>,他的约束是IdentityUser实体类

 

​​​​​​​​​​​​​​实体类IdentityUser<string,IdentityUserLogin,...>

 

IdentityUser实体类继承自IdentityUser<string,IdentityUserLogin,...>实体类。IdentityUser<string,IdentityUserLogin,...>实体类实际上就和我们平常写的model实体类没什么区别。

这个类和数据表字段也是一一对应的

 

​​​​​​​ApplicationUser(identityModel.cs)

 

​​​​​​​增加实体类IdentityUser不存在的属性

可以看到ApplicationUser继承了1.4.6中的IdentityUser类,那么我们可以在ApplicationUser里面直接写上我们想要的属性,来增加IdentityUser中没有的顺序拧,比如上面的public int Age{get; set;}

 

​​​​​​​用户声明userIdentity,可以用来检查用户权限

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章